4

What is the interpretation of the whitespace in this command foo= bar ?

Why are foo=bar and foo= bar interpreted differently

Example (Ubuntu bash)

developer@1604:~$ foo=bar
developer@1604:~$ foo= bar
The program 'bar' is currently not installed. You can install it by typing:
sudo apt install bar

4 Answers4

10

This is syntax: Bash variables get inititalized with the value that follows immediately after the assignment operator =. That's simply the way it is...

When you do foo= bar then you are assigning an empty string to the variable foo and then execute the command bar.

  • Note that such assignment to foo will remain valid only for the duration of bar, and doesn't affect further commands in the session. – Ruslan Aug 16 '17 at 09:33
4

When you do foo= bar then you are assigning an empty string to the environment variable foo and then execute the command bar. It can be used to pass environment variables to a new execution.

Why it is different

Space is the delimiter character (used to separate things): adding a space makes it be two things foo= and bar. The first is an assignment, the second is telling bash to find a file and execute it. The first is optional so you will usually only see the second. The second is not optional: If you only see an assignment, it does something slightly different, it assigns to a shell variable (not an environment variable, unless preceded with export).

2

In shell linux/unix command, when you're typing foo=bar, you put the string bar in the foo variable. For checking you could type echo $foo that should return bar this is what the variable foo contains

When you're type foo= bar with the space, the shell interpreter put nothing, in the foo variable and handle bar as a application command through the PATH variable. As the shell didn't find bar command in the PATH variable and you're using Ubuntu distribution looks like, the system is asking if you want to install the bar application. It's just a pop up message when you application is not find. You could check the PATH variable with this command : echo $PATH. More info about this system variable Here

dubis
  • 1,460
2

foo= bar : before executing the command bar , assign foo to empty. The correct way is foo=bar No space after the variable name and the assigned value.

GAD3R
  • 66,769