Assignment of variables with space after the (=) sign?
Solution 1:
In the example PWD= /bin/pwd
, the variable PWD
is set to the empty string before executing the command /bin/pwd
. The change only takes effect for that line.
This can be useful to make a temporary change to a variable for the purposes of running a command, without affecting the original value. Another example of this would be when using read
, to set a different IFS
:
IFS=, read a b c <<<"comma,separated,list"
This sets the field separator to a comma so that a
, b
and c
are read correctly. After this line, IFS
returns to the default value, so the rest of the script isn't affected.
Perhaps on some systems, the output of the command pwd
is affected by the value of the variable PWD
, so doing this prevents problems caused by PWD
being overwritten elsewhere.
Solution 2:
We are not talking about two different things here.
If we had
PWD=/bin/pwd
we would assign /bin/pwd
to PWD
.
But
PWD= /bin/pwd
means that we call /bin/pwd
with PWD
set to the empty string. This assignment only affects the sub process, not the current one.
Solution 3:
PWD= pwd
This syntax assigns the empty value to the variable PWD
for the duration of the pwd
command.
PWD=ick
echo "$PWD"
This assigns PWD
for the remainder of the script.
PWD=ick pwd
echo "$PWD"
This assigns PWD
only for the duration of the pwd
command; the echo
will echo the value which was in effect before and after the pwd
invocation.
PWD=
This simply assigns the empty value to PWD
.
Pathologically,
PWD = ick
attempts to run the command PWD
with the arguments =
and ick