Unix Special Variables
Special Variables
Sr. No. | Variable & Description |
---|---|
1. | $0 – The filename of the current script. |
2. | $n – These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on). |
3. | $# – The number of arguments supplied to a script. |
4. | $* – All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2. It takes input as string. For difference, run the script /unix-variable-and-difference/ |
5. | $@ – All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2. It takes input as array. |
6. | $? – The exit status of the last command executed. Output ‘0’ if all successful ‘1’ if unsuccessful. |
7. | $$ – The process number of the current shell. For shell scripts, this is the process ID under which they are executing. |
8. | $! – The process number of the last background command. |
9. | $1-$9 – Command Line Arguments |
Example
File Name: specialchar.sh
#!/bin/sh
echo “File Name: $0”
echo “First Parameter : $1”
echo “Second Parameter : $2”
echo “Quoted Values: $@”
echo “Quoted Values: $*”
echo “Total Number of Parameters : $#”
Command to Run: ./specialchar.sh Nishant Munjal