Sunday, July 25, 2021

List of special bash parameter used in Unix or Linux script - Example

Meaning of bash parameter used in Unix script
Many of us use a bash script for doing housekeeping and other stuff but occasionally and not much aware of special bash parameters. When I was new to the bash shell and Linux and looking for some already written bash script I used to get baffled with special bash symbols like $@, $_, $1, etc. I did know that they are bash parameter and has special meaning but I don't have all meanings of special bash parameter on top of my head and I always go to Google and search endlessly for those special bash parameter, sometimes I got and found the meaning of those special bash script parameters quickly but sometimes I need to spend some time to get what I am actually looking for, so I thought to document meanings and expansion of those special bash parameters or bash script parameters.

This bash script parameter tutorial is in continuation of my earlier UNIX tutorials find examples in UNIX, grep command examples in UNIX, and UNIX networking commands tutorial if you haven’t read already you may find them interesting and useful.

The list which I am going to share with you guys today is what I have accumulated over the years. 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.

spcial bash parameters and bash script parameters

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.


$ 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 J


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.
 
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.


Some more UNIX Command tutorials

12 comments :

anonymous said...

Thanks for this nice post , I was looking meaning for following 8 bash parameters "$!, $?, $@, $#, $-,$*, $$, $_" and I got that in one place. cheers buddy.

Rashmi said...

Hi Javin,
Can you explain what is difference between $* and $@ , I still not able to understand $* quite well. Thanks

Anonymous said...

In simple words you could probably say that $# is used to count number of positional parameters or arguments passed to script.

Anonymous said...

good explanation!
thanks Javin.

-- srihari konakanchi

Anonymous said...

Thanks...all BASH parameters and explanation in one place.

Unknown said...

add a example of each parameter.....

this is also very good but more effective if u wont than add a new practical example in this one .....

thank u ........

Anonymous said...

Special bash characters and there meaning...
Hmm... Did you mean "not here meaning" or "their meaning"?

Anonymous said...

Spelling: "Special bash characters and their meaning" - perhaps?

John said...

Typo: "$0 is one of the most used bash parameters"
Fix: "$? is one of the most used bash parameters"

Still reading through this. Very helpful.

Unknown said...

Thanks much for this very useful post,If possible please help to post an example with all the special characters included in it
Also helpful if we see some more info about arithmetic expressions

javin paul said...

Hello @Unknonw, that's a good idea, noted. will provide examples sometime.

Anonymous said...

In all the online tutorials I see nobody talks about one very important thing:
I'm having problems with a .sh script because I put variables like: varname1-varnameext1. This causes bash not to recognize the variable as there is a dash "-" in the variable name.
I didn't find anyone on the internet who explained about this condition.
I had to try and fix it myself through trial and error until I got it right.
Then I found that: the variable name cannot contain a dash "-", but an underline "_".
So my variable happens to be defined with an underline instead of a dash: varname1_varnameext1.
Now all perfect 100%!

Post a Comment