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
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
# without export i=toto cat > f << END #!/bin/bash echo \$i END chmod +x f ./f # with export export i=toto ./f
Print Working Directory
This variable contains the name of the current working directory.
This variable contains the name of the previous directory (the one preceding the current one).
This variable contains the name of the default user directory (its home directory).
This variable contains the name of the shell command-line interpreter.
Cette variable contient le nom (login) de l'utilisateur. This variable contains the name of the user (his/her login).
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
PATHif 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
PATHis executed)), otherwise, an error message is issued.
f which f ./f which f echo $PATH export PATH=$PATH:. which f f
which gives the path of the command (i.e., where it is located).
If a command is located in different directories, the first occurence in
PATH is executed.
-a option of the
which command displays all locations in
PATH of a given command.
In a similar manner as
LD_LIBRARY_PATH contains the directories where to find software libraries.
This is important for compiled programs that make use of shared libraries.
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
#, @, 1, 2, 3, ...¶
A shell script can accept arguments. They are placed in a list of arguments accessible by variables.
||number of arguments of the script|
||first argument of the script|
||second argument of the script|
||N-th argument of the script|
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