Standard Environment Variables used with Unix Shell Scripts
Different shells set up various environment variables when launching a shell script.
The table that follows shows some of the more useful environment variables set up by the bash shell. Any of these can be accessed in a bash shell script. Additional variables can be found in the bash man page.
You can access the value of any of these variables using the standard format with a dollar sign. For example, to display the current working directory, use this command:
The following table shows some of the environment variables used by the tcsh.
Numerous additional variables can be found in the tsch manual page.
env env [options] [variable=value ... ] [command]
Display the current environment or, if environment variables are specified, set them to a new value and display the modified environment.
If command is specified, execute it under the modified environment.
-, -i, --ignore-environment
Ignore current environment entirely.
-u name, --unset name
Unset the specified variable.
Linux Shell Scripting
The ENV File
When you start the shell, one of the first things it does is look in your environment for a variable called ENV.
If it finds it and it is non-null, the file specified by ENV will be executed, much like the .profile is executed when logging in.
The ENV file contains commands to set up the shell’s environment. Throughout this chapter, we will mention various things that you may want to put into this file. If you do decide to have an ENV file, you should set and export the ENV variable inside your
$ cat .profile
Note the shortcut in use above: Instead of assigning the variable a value, then calling export, it turns out you can do both on the same line for efficiency.
For Bash users, the ENV file is read only when Bash is invoked with the name sh, with the
--posix command-line option,
or after set –o posix is executed (all of which force POSIX standard compliance).
By default, when a non-interactive Bash shell is started (for example, when you run a shell program),
it reads commands from the file specified by the BASH_ENV environment variable, and when an interactive Bash shell is started
(for example, by typing bash at the command prompt), it does not.
If you are running an older system, you should also export a variable called SHELL inside your
$ grep SHELL .profile
SHELL=/bin/ksh ; export SHELL
This variable is used by certain applications (such as vi) to determine what shell to start up when you execute a shell escape. In such cases, you want to make sure that each time you start up a new shell, you get the shell you want and not an older Bourne shell.
Probably, though, SHELL will already be set by your login shell. You can test it with
$ echo $SHELL
Also note that the previous example demonstrates yet another way to set and export a variable, this time by having two separate commands on the same line, separated by a semicolon. Why the inconsistency?
Because Unix is so flexible that you will find it is common for shell programs you read to accomplish the same task multiple ways.