kaneda
kaneda

Reputation: 6187

Assignment of variables with space after the (=) sign?

In Bash, assigning values to variables is done using T=content, with no spaces before or after the equal sign.

Despite that I've seen the following in a shell script PWD= /bin/pwd containing a space on the right side of the equals sign.

What's the purpose of it have a space?

Upvotes: 36

Views: 15850

Answers (4)

Pavan
Pavan

Reputation: 662

When bash or most other shells see the below line:

PWD= /bin/pwd

the shell parses the command line from left to right. It breaks down the command line into two fields: PWD= and /bin/pwd since they're separated by unquoted space. When it goes back to parsing the first field PWD= it finds an unquoted = and so, shell treats it as a variable assignment/initialization. It thinks PWD as the variable name and any string immediately following the = sign until the space as the variable value. In this case, it would be an empty string since there is just a space after = sign and unquoted space is an argument separator in shell. So, PWD value would be '' (empty string) and by the way, this only affects the immediate command /bin/pwd as others have mentioned.

If there was a space before = as well, then shell would see 3 fields in total

PWD = /bin/pwd

Shell would treat first field PWD as either alias or function or command by searching in PATH locations. It would treat the other two fields = and /bin/pwd as arguments to the first command or function

Upvotes: 0

tripleee
tripleee

Reputation: 189679

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

Upvotes: 12

Tom Fenech
Tom Fenech

Reputation: 74685

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.

Upvotes: 24

glglgl
glglgl

Reputation: 91109

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.

Upvotes: 5

Related Questions