Meaning of bash parameter used in Unix script
Many of us use bash script for doing housekeeping and other stuff but occasionally and not much aware of special bash parameters. When I was new to bash shell and Linux and looking on 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, some time I got and found meaning of those special bash script parameters quickly but sometime I need 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 are 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 special bash parameters?
Special bash parameters are those parameters which bash shell treats especially when encounters in bash script in Linux or UNIX system. 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 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 is quite tricky specially 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 there 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 number of positional parameters in decimal. |
$0 | $0 bash parameter is used to reference name of the shell or shell script. so you can use this if you want to print name of shell script. |
$- | $- (dollar hyphen) bash parameter is used to get current option flags specified during 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 parameter and used to get the exit status of the most recently executed command in 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 mails. |
$@ | $@ (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 value of each bash parameter separated by the first character of the IFS special environment variable. |
Example of passing parameter to bash script in Unix:
Many times we require passing parameter to bash script. Now let’s see an example of bash script on which we will pass bash parameter from command prompt and will access them inside 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 lets 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 first parameter $1 and each of those will be separated by space.
For example if your 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 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 there meaning
We have just seen special bash script parameters and there 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 is 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 name of command in bash script. |
$name | $name will print the value of variable “name” defined in script. |
$n | $n will print value of nth argument provided to bash script (n ranges form 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 sub shell |
` ` | 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 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 which makes the script more complex or reading but at 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
Tidak ada komentar:
Posting Komentar