Environment variables and PATH

Video 108 of 140 in Virtualization Foundations, in the unit “The shell and the filesystem tree”. 2:49, with English and Hindi audio.

A program can be installed on a server and still fail with command not found. The usual cause is a single setting called PATH, which tells the shell where to look for programs. This article explains environment variables, how export passes one on to other programs, and how PATH works. Once you know this, a missing command becomes a question you can answer.

What you will learn

A nightly job that cannot find its tool

A nightly job on a Harbor Point Logistics server runs a backup tool, which is installed in its own folder. Harbor Point is a fictional logistics firm, used for training examples. One night the job reports command not found. The tool is installed, and it works when an engineer types it by hand.

The shell looks for programs only in the folders listed in PATH. The job's list did not include the backup tool's folder, so the search never reached it.

Environment variables and export

An environment variable is a named value that the shell can pass to the programs it starts. The export command makes a variable visible to those programs. Here, GREETING is exported, so a new bash started from this shell can read it.

Set GREETING and export it to programs started from this shell
export GREETING=hello

A plain assignment, without export, stays inside the shell that made it. A child shell does not see it.

Start a child shell and try to read a plain variable
bash -c 'echo $NAME'

Note: This prints an empty line, because the child shell cannot see NAME.

How PATH is searched

PATH is one such variable. It is a list of folders, joined by colons, and the shell searches them one at a time. It checks the first folder, moves on when it finds no match, and runs the first program whose name matches.

Print the folders the shell searches
echo $PATH

Note: The output is a list of folders joined by colons. Yours will differ from another server's.

Where a variable is seen

Try it yourself

You need a Linux terminal. You will set a variable, read it back in three ways, and then see how a plain variable behaves in a child shell.

  1. Set GREETING and export it, so programs started from this shell can read it.

    export GREETING=hello
  2. Print the variable from this shell.

    echo $GREETING

    What you should see: The shell prints hello.

  3. Ask printenv for the one variable by name.

    printenv GREETING

    What you should see: The shell prints hello.

  4. Start a new bash and read GREETING inside it. The exported value is passed on.

    bash -c 'echo $GREETING'

    What you should see: The shell prints hello.

  5. Set NAME without export. This value stays in this shell only.

    NAME=local
  6. Start a new bash and read NAME. The child shell cannot see the plain variable.

    bash -c 'echo $NAME'

    What you should see: An empty line is printed, because the child shell cannot see NAME.

  7. Print PATH, the list of folders the shell searches for programs.

    echo $PATH

    What you should see: A list of folders joined by colons. Yours will differ.

Terms

Environment variable
A named value that the shell can pass to the programs it starts, so they can read it.
PATH
A variable holding a list of folders, joined by colons, that the shell searches in turn for a program.
Export
The shell command that makes a variable visible to the programs the shell starts.

Check yourself

Why does an exported variable reach a new bash, but a plain one does not?

Export passes the variable on to the programs the shell starts. A plain variable stays inside the shell that made it.

Why can a backup tool be installed and still show command not found?

The shell searches only the folders listed in PATH. If the tool's folder is not in that list, the shell never finds it.

What does the PATH setting do?

A variable that holds a list of folders, joined by colons. The shell searches those folders in turn when you type a program name.

Previous: Pipes and redirection. Next: History, completion and shortcuts.