Reputation: 670
I have two test snippet code, as follows:
# snippet code1
a=100 b=200 # this line confuses me
echo $a # print "100"
# snippet code2
a=100 echo "sth else" # this line confuses me
echo $a # print empty line
What is the difference? How to explain?
Thanks for your answer!
Upvotes: 0
Views: 83
Reputation: 52506
The first one is multiple assignment statements on the same line; it's the same as
a=100
b=100
echo "$a
The second one is setting the environment for the command that follows; for example:
$ unset a
$ a=abc env | grep -w a
a=abc
$ declare -p a
-bash: declare: a: not found
The environment is set for just the command that follows.
Notice that
a=abc echo "$a"
doesn't work because the variable is expanded before the command is run.
Upvotes: 2