System Variables used in Scripting
The system variables available to you depend on the shell version that you are using.
The Bourne shell (sh) probably has the fewest of these variables. Newer shells, which you are more likely to use,
such as bash or tcsh include many additional variables.
The bash shell includes the following special system variables (note that additional environment variables are provided in addition to these):
Special parameter in the bash |
Meaning |
$* |
Equal to all command-line parameters combined into one word, without spaces between them |
$@ |
Equal to all command-line parameters as a single string of text, just as they appear on the command line.- |
$# |
The number of parameters on the command line (and thus the number of positional variables defined).- |
$? |
The status of the most recently executed program in the shell where the script was started.- |
$-
|
The current options flags for the shell (set using the builtin command) |
$$ |
The UNIX process ID of the shell running the script |
$! |
The UNIX process ID of the most recently executed background process executed by the shell |
You can use the echo
command to print the value of any of these variables to the screen. For example:
[[delete break]]
echo $$[[delete break]]
displays the current process ID of the shell running the script.
You can also use an if/then
statement to test for a certain value of the variable or use the variable within other statements. For example, if you need to create a unique filename, use the value of the $$ variable as part of the filename. Because the process ID is unique number, the filename will be unique also.
Additional environment variables for both bash and the tcsh are described in the next lesson.