4

What happens when this command is executed?

PATH =$PATH:`pwd`

Error: Nothing happens when i execute the command in the terminal.

15

If you execute the command in your question, you should get an error message:

$ PATH =$PATH:`pwd`
bash: PATH: command not found

If the space is a typo, and you actually run this:

PATH=$PATH:`pwd`

Then that will add the current directory (pwd is a command that prints the path of the current directory, and `pwd` will be replaced with the output of pwd) to the PATH variable for the duration of your current shell session (util you close the terminal). To illustrate:

$ pwd
/home/terdon/foo  ## I am in /home/terdon/foo

$ echo $PATH ## the current PATH
/home/terdon/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

$ PATH=$PATH:`pwd`
$ echo $PATH
/home/terdon/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/terdon/foo

Note how the current directory, /home/terdon/foo has been added to the end of the PATH variable.

2

The error

As far as your original command goes, terdon's answer already covers very well what is happening and what should happen. Let's analyze that slightly in more detail.

What you're trying to do is variable assignment, which should take form of:

varName=value

However, because of the space this is treated as a command PATH with command line argument consisting of a = then contents of variable $PATH and command-substitution of pwd, all joined in to same string:

PATH =$PATH:`pwd`

Of course, there is no such command as PATH, hence you will get an error.

Shell treats simple commands and their arguments as list of one or more space-separated tokens (with perhaps optional variable assignment before that). In other words, the form should be:

[VAR=value] word [arg1 [arg2 [arg3] ]...]

Of course, this is not what is desired in this case.

The proper command

So what should have been done is this:

PATH=$PATH:`pwd`    

Here we have proper variable assignment, where in accordance with the expansion order, bash shell would first perform variable expansion, i.e. replace $PATH with whatever you have in that variable currently, then perform command-substitution where it runs pwd and replaces it with output of pwd. In the end, all that gets assigned back to PATH variable.

A few key observations:

  • Since this is variable assignment, you don't need to quote $PATH variable, but usually you should to avoid undesirable behavior. See When is Double Quoting Necessary
  • backticks form of command substitutions is not recommended nowadays. In this particular case, it's OK, but in general backticks don't lend themselves nicely to nesting and multi-line commands.
  • PATH variable is used by shell to find commands, and the order of directories on that list sets the precedence to where commands would be searched. So for example, when you execute a command the shell would first check whether the command is built-in, if it's not then it would look through all directories on the PATH variable. The PATH=$PATH:$(pwd) would make shell traverse everything else, and only then get to the directory that was given by pwd. By contrast, PATH=$(pwd):$PATH would start the search from the directory returned by pwd first. So order of the assignment matters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy