Here I accumulated the most useful and frequently used special Bash characters and parameters. This list of special bash parameters is by no means complete and only contains some of the bash script parameters which I have encountered, so please contribute any bash parameter which is not in this list and you found useful.
What are the special bash parameters?
Special bash parameters are those parameters that bash shell treats especially when encounters in a bash script in Linux or UNIX system. The important point of these bash script parameters is that they can only be referenced and you can not assign any value to them.
Apart from special bash parameters I have also listed down the meaning of special bash characters for quick reference e.g. “&” “>” “>>“, these special bash characters are quite useful while working in bash shell in UNIX or Linux environment.
I know remembering these special bash parameters and special characters are quite tricky especially when you write bash script occasionally but at the same time, you need to understand it to read existing bash scripts written by someone like bash script experts.
What I do is I keep this in a place which is quite handy and just search whenever I come across any special bash parameters.
Special bash parameters and their meaning
Special bash parameter | Meaning |
$! | $! bash script parameter is used to reference the process ID of the most recently executed command in background. |
$$ | $$ is used to reference the process ID of bash shell itself |
$# | $# is quite a special bash parameter and it expands to a number of positional parameters in decimal. |
$0 | $0 bash parameter is used to reference the name of the shell or shell script. so you can use this if you want to print the name of shell script. |
$- | $- (dollar hyphen) bash parameter is used to get current option flags specified during the invocation, by the set built-in command or set by the bash shell itself. Though this bash parameter is rarely used. |
$? | $0 is one of the most used bash parameters and used to get the exit status of the most recently executed command in the foreground. By using this you can check whether your bash script is completed successfully or not. |
$_ | $_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails. |
$@ | $@ (dollar at the rate) bash parameter is used to expand into positional parameters starting from one. When expansion occurs inside double-quotes, every parameter expands into separate words. |
$* | $* (dollar star) this is similar to $@ special bash parameter only difference is when expansion occurs with double quotes, it expands to a single word with the value of each bash parameter separated by the first character of the IFS special environment variable. |
Example of passing a parameter to bash script in Unix:
Many times we require a passing parameter to bash script. Now let’s see an example of a bash script on which we will pass bash parameter from the command prompt and will access them inside the bash script. This example will help you to understand the use of special bash parameters much better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$ cat bashparameterexample.sh #!/bin/bash if [ $# -lt 2 ] -- $# is used for number of arguments then echo "Usage: $0 arg1 arg2" exit fi echo -e "\$1=$1" -- $1 is used to access first parameter echo -e "\$2=$2" -- $2 is used to access second parameter |
Now let’s invoke the bash script without parameter, it will print the “usage “statement because $# is less than 2, now you see how useful “$#” is for writing script.
1 2 3 4 5 |
Javin@stocktrading ~ $ ./bashparameterexample.sh Usage: ./bashparameterexample.sh arg1 arg2 |
Now lets pass two parameters to the script and access them via $1 and $2 its really fun writing bash script once you are familiar with bash parameters.
1 2 3 4 5 6 7 |
Javin@stocktrading ~ $ ./bashparameterexample.sh 23 44 $1=23 $2=44 |
Difference between $* and $@ bash parameters
$* and $@ both are used to access full list of positional parameters supplied during invocation of bash script and you can iterate through them using bash for loop. $* and $@ are special bash parameters and if used without double quotes then both will behave identical i.e. they will provide the list of positional parameter starting from the first parameter $1 and each of those will be separated by space.
For example, if you’re provide 5 parameters during invocation of bash script $@ and $* will expand into $1 $2 $3 $4 $5
But if you double quotes then they will behave differently, in the case of $* individual parameters will be separated by first char of IFS characters while in case of $@ individual parameters will appear in quotes and separated by space.
Special bash characters and their meaning
We have just seen special bash script parameters and their meaning and now we will some special bash characters and what they mean when used inside bash script or in bash shell command prompt. We will also see special bash characters called redirection characters which are used to redirect input, output etc
Special bash character | Meaning |
# | # is used to comment a single line in bash script |
$$ | $$ is used to reference process id of any command or bash script |
$0 | $0 is used to get the name of the command in a bash script. |
$name | $name will print the value of variable “name” defined in the script. |
$n | $n will print the value of nth argument provided to bash script (n ranges from 0 to 9) e.g. $1 will print first argument. |
> | > is used to redirect output |
>> | >> can be used to Append to file |
< | < will redirect input |
[ ] | [] are for matching any characters enclosed |
( ) | Execute in subshell |
|
Another powerful special bash character which is used to substitute output of enclosed command |
” “ | Partial quote (allows variable and command expansion) |
‘ ‘ | Full quote (no expansion) |
\ | Quote following character |
| | Pipe output of one command to another most useful and immensely power bash character |
& | & is used to run any process in the background. |
; | ;(semi colon ) is used to separate commands on same line |
* | * is used to match any character(s) in filename |
? | ? is for matching single character in filename |
Knowledge of special bash parameters and special bash characters will make you more comfortable while reading and understand already written bash scripts. It is this cryptic bash parameter that makes the script more complex or reading but at the same time can be immensely useful if know how to use them. These special bash parameters can be used in any bash script.