Environment Variables

When a parent process creates a child process, the first transfers to the second a certain number of variables called environment variables. This allows the parent to pass values that will be known by the child process.

To know all the environment variables known by the current process, we can use the command env. A number of these variables are of great importance for the operation of the command interpreter:

To create an environment variable, simply use the command export.

# without export
i=toto
cat > f << END
#!/bin/bash
echo \$i
END
chmod +x f
./f
# with export
export i=toto
./f

PWD

Print Working Directory

This variable contains the name of the current working directory.

OLDPWD

This variable contains the name of the previous directory (the one preceding the current one).

HOME

This variable contains the name of the default user directory (its home directory).

SHELL

This variable contains the name of the shell command-line interpreter.

USER

Cette variable contient le nom (login) de l'utilisateur. This variable contains the name of the user (his/her login).

PATH

This variable contains the list of the directories that contains executable commands.

Each time a command is called:

  • either the path (absolute or indirect) of the command is given and then the interpreter fetches the command in the specified directory

  • either the interpreter checks in the directories designated by PATH if it finds the given command. If the command is found in one of the directories specified by PATH, then it is executed((the first occurence in PATH is executed)), otherwise, an error message is issued.

f
which f
./f
which f
echo $PATH
export PATH=$PATH:.
which f
f

The command which gives the path of the command (i.e., where it is located).

which tar

If a command is located in different directories, the first occurence in PATH is executed. The -a option of the which command displays all locations in PATH of a given command.

LD_LIBRARY_PATH

In a similar manner as PATH, LD_LIBRARY_PATH contains the directories where to find software libraries.

This is important for compiled programs that make use of shared libraries.

The ldd command lists for a given compiled program the set of shared libraries that are required by the program to run.

In case of a missing software library, a command cannot be executed. By changing the LD_LIBRARY_PATH variable, path(s) to missing libraries can be added accordingly.

which ls
ldd /bin/ls

$

Every process is identified by a unique PID number (Process IDentifier).

This PID number can be obtained by the variable $.

echo $$

#, @, 1, 2, 3, ...

A shell script can accept arguments. They are placed in a list of arguments accessible by variables.

variable meaning
$# number of arguments of the script
$@ all arguments
$0 script name
$1 first argument of the script
$2 second argument of the script
$n N-th argument of the script

The shift command is used to decrement from 1 the list of arguments of a script.

#!/bin/bash

echo first argument: $1
echo second argument: $2
echo third argument: $3
echo shift
shift
echo first argument: $1
echo second argument: $2
# script use:
./script.sh a1 a2 a3