LDP/LDP/guide/docbook/abs-guide/badread.sh

64 lines
1.2 KiB
Bash

#!/bin/bash
# badread.sh:
# Attempting to use 'echo and 'read'
#+ to assign variables non-interactively.
# shopt -s lastpipe
a=aaa
b=bbb
c=ccc
echo "one two three" | read a b c
# Try to reassign a, b, and c.
echo
echo "a = $a" # a = aaa
echo "b = $b" # b = bbb
echo "c = $c" # c = ccc
# Reassignment failed.
### However . . .
## Uncommenting line 6:
# shopt -s lastpipe
##+ fixes the problem!
### This is a new feature in Bash, version 4.2.
# ------------------------------
# Try the following alternative.
var=`echo "one two three"`
set -- $var
a=$1; b=$2; c=$3
echo "-------"
echo "a = $a" # a = one
echo "b = $b" # b = two
echo "c = $c" # c = three
# Reassignment succeeded.
# ------------------------------
# Note also that an echo to a 'read' works within a subshell.
# However, the value of the variable changes *only* within the subshell.
a=aaa # Starting all over again.
b=bbb
c=ccc
echo; echo
echo "one two three" | ( read a b c;
echo "Inside subshell: "; echo "a = $a"; echo "b = $b"; echo "c = $c" )
# a = one
# b = two
# c = three
echo "-----------------"
echo "Outside subshell: "
echo "a = $a" # a = aaa
echo "b = $b" # b = bbb
echo "c = $c" # c = ccc
echo
exit 0