1# 2# varenv.sh 3# 4# Test the behavior of the shell with respect to variable and environment 5# assignments 6# 7expect() 8{ 9 echo expect "$@" 10} 11 12a=1 13b=2 14c=3 15d=4 16e=5 17f=6 g=7 h=8 18 19a=3 b=4 $CHMOD $MODE $FN 20 21# This should echo "3 4" according to Posix.2 22expect "3 4" 23echo $a $b 24 25set -k 26 27# Assignment statements made when no words are left affect the shell's 28# environment 29a=5 b=6 $CHMOD c=7 $MODE d=8 $FN e=9 30 31expect "5 6 7 8 9" 32echo $a $b $c $d $e 33 34$CHMOD f=7 $MODE g=8 $FN h=9 35expect "7 8 9" 36echo $f $g $h 37 38set +k 39 40# The temporary environment does not affect variable expansion, only the 41# environment given to the command 42 43export HOME=/usr/chet 44expect $HOME 45echo $HOME 46 47expect $HOME 48HOME=/a/b/c /bin/echo $HOME 49 50expect $HOME 51echo $HOME 52 53# This should echo /a/b/c 54expect /a/b/c 55HOME=/a/b/c printenv HOME 56 57set -k 58 59# This should echo $HOME 9, NOT /a/b/c 9 60 61expect "$HOME" 62HOME=/a/b/c /bin/echo $HOME c=9 63expect "$HOME 7" 64echo $HOME $c 65 66# I claim the next two echo calls should give identical output. 67# ksh agrees, the System V.3 sh does not 68 69expect "/a/b/c 9 /a/b/c" 70HOME=/a/b/c $ECHO a=$HOME c=9 71echo $HOME $c $a 72 73expect "/a/b/c 9 /a/b/c" 74HOME=/a/b/c a=$HOME c=9 75echo $HOME $c $a 76set +k 77 78# How do assignment statements affect subsequent assignments on the same 79# line? 80expect "/a/b/c /a/b/c" 81HOME=/a/b/c a=$HOME 82echo $HOME $a 83 84# The system V.3 sh does this wrong; the last echo should output "1 1", 85# but the system V.3 sh has it output "2 2". Posix.2 says the assignment 86# statements are processed left-to-right. bash and ksh output the right 87# thing 88c=1 89d=2 90expect "1 2" 91echo $c $d 92d=$c c=$d 93expect "1 1" 94echo $c $d 95 96# just for completeness 97unset d c 98expect unset 99echo ${d-unset} 100 101# no output 102export a 103a=bcde 104export a 105/bin/true 2>/dev/null 106 107func() 108{ 109 local YYZ 110 111 YYZ="song by rush" 112 echo $YYZ 113 echo $A 114} 115 116YYZ="toronto airport" 117A="AVAR" 118echo $YYZ 119echo $A 120A=BVAR func 121echo $YYZ 122echo $A 123 124export A 125# Make sure expansion doesn't use assignment statements preceding a builtin 126A=ZVAR echo $A 127 128XPATH=/bin:/usr/bin:/usr/local/bin:. 129func2() 130{ 131 local z=yy 132 local -a avar=( ${XPATH//: } ) 133 echo ${avar[@]} 134 local 135} 136 137avar=42 138echo $avar 139func2 140echo $avar 141 142# try to set an attribute for an unset variable; make sure it persists 143# when the variable is assigned a value 144declare -i ivar 145 146ivar=10 147 148declare -p ivar 149unset ivar 150 151# export an unset variable, make sure it is not suddenly set, but make 152# sure the export attribute persists when the variable is assigned a 153# value 154export ivar 155echo ${ivar-unset} 156 157ivar=42 158declare -p ivar 159 160# make sure set [-+]o ignoreeof and $IGNOREEOF are reflected 161unset IGNOREEOF 162set +o ignoreeof 163set -o ignoreeof 164if [ "$IGNOREEOF" -ne 10 ]; then 165 echo "./varenv.sh: set -o ignoreeof is not reflected in IGNOREEOF" >&2 166fi 167unset IGNOREEOF 168set +o ignoreeof 169 170# older versions of bash used to not reset RANDOM in subshells correctly 171[[ $RANDOM -eq $(echo $RANDOM) ]] && echo "RANDOM: problem with subshells" 172 173# make sure that shopt -o is reflected in $SHELLOPTS 174# first, get rid of things that might be set automatically via shell 175# variables 176set +o posix 177set +o ignoreeof 178set +o monitor 179echo $- 180echo ${SHELLOPTS} 181shopt -so physical 182echo $- 183echo ${SHELLOPTS} 184 185# and make sure it is readonly 186readonly -p | grep SHELLOPTS 187 188# This was an error in bash versions prior to bash-2.04. The `set -a' 189# should cause the assignment statement that's an argument to typeset 190# to create an exported variable 191unset FOOFOO 192FOOFOO=bar 193set -a 194typeset FOOFOO=abcde 195 196printenv FOOFOO 197 198# test out export behavior of variable assignments preceding builtins and 199# functions 200$THIS_SH ./varenv1.sub 201 202# more tests; bugs in bash up to version 2.05a 203$THIS_SH ./varenv2.sub 204 205# make sure variable scoping is done right 206tt() { typeset a=b;echo a=$a; };a=z;echo a=$a;tt;echo a=$a 207