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, "$@" 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.

Share this

Related Posts

Previous
Next Post »

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