30

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?

AJF
  • 11,767
  • 2
  • 37
  • 64
kaneda
  • 5,981
  • 8
  • 48
  • 73
  • 1
    did you run the script and see what the PWD holds? It will be empty and you will see present working directory printed when you run the script? – SMA Nov 17 '14 at 11:52

4 Answers4

20

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.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Maybe that's for different shells other than bash compatibility? That's the author explanation: `# The following circumlocution (repeated below as well) ensures that we record the true directory name and not one that is faked up with symlink names.` – kaneda Nov 17 '14 at 12:05
  • 1
    Yes, from that comment, it sounds like it is possible that `$PWD` gets assigned an incorrect value in some cases and `pwd` will read this value if it is set, rather than work out the real path, on some other shells. – Tom Fenech Nov 17 '14 at 12:10
  • 3
    PWD is *not* unset. It is explicitly being set to the empty string. There is a difference. For example, consider the difference between `${FOO-bar}` and `${FOO:-bar}`. – William Pursell Nov 17 '14 at 14:32
10
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

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 3
    For what it's worth, by Bash's `pwd` command reports the current directory regardless of the value of `PWD`. – tripleee Nov 17 '14 at 11:52
  • Incidentally, hardcoding `/bin/pwd` isn't usually necessary, and might be a bad idea. Scripts should trust the user to have a sane `PATH`, and allow for them to override system commands in some circumstances (mocking commands in a test script immediately comes to mind as one common scenario). Of course, here, you might want to ensure that any shell built-in command `pwd` is bypassed in favor of an external binary? But in the general case I would dissuade from doing this. – tripleee Apr 22 '17 at 08:38
5

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.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

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

Pavan
  • 632
  • 5
  • 9