Sharing Knowledge | System Admin Articles

Special Shell Variables - Useful for Scripting



Below are the special shell variables. These are important to know for everyone, especially who is willing to learn shell scripting. Hope the list helps.

Name
Description
$1 - $9
These variables are referring the parameters passed to command or script. $1 refers to the first argument and $2 refers second and so on.
$0
The name of the command or script currently being executed.
$#
The number of arguments passed to the command/script or invocation of the shell.
$?                     
The exit status of the last command executed is given as a decimal string.  When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.
$$
The process number of the currently executing command or script. - Useful for including in filenames, to make them unique.
$!
The process ID of the last command runs in the background.
$-
The current options supplied to the command or script.
$*
A string containing all the arguments passed to the command/script or shell, starting at $1. When quoted, "$*" is a single word, comprising all the arguments to the shell, joined together with spaces. For example ‘a b' c becomes "a b c".
Same as above, except when quoted. When quoted, "[email protected]" is identical to the arguments received by the shell, the resulting list of words completely match what was given to the shell. For example '1 2' 3 becomes "1 2" "3"

Apart from these, there are some standard variables which are set through .profile or .bashrc. Try "env" command to view all of them.


Labels: scripts, Shell

My Profile PhotoAbout the Author

I'm Parthiban, An UNIX System Admin by Profession. I'm Experienced in Linux/Unix System Administration and Scripting. I have done lot of work on the infrastructure Mgmt side in Linux, UNIX and Windows system administration, Hardware, Storage and Data center. I'm blogging since 2008.
Follow Me On Twitter or On Facebook

0 Comments for "Special Shell Variables - Useful for Scripting"

What do you think about this Article? Add your Opinion..!

Back To Top