BASH inbuilt VARIABLES for easy scripting
Written by anil on July 15th, 2007 in Bash Scripting, Linux.
"BASH inbuilt VARIABLES for easy scripting" as the title says this small tutorial will make bash scripting easier. Do you know its possible to control "for" loop if you set a inbuilt variable . . . I won't say that variable now …. Please continue reading to know that ….
First we start with $0 $1 .. $9, $*, $@ and $#. To explain these variables lets write a simple script called "test-param.sh".
#!/bin/bash
echo "The script file is $0"
echo "The first parameter passed to $0 is $1 and the second one is $2"
echo "$@ are the total parameters passed to $0"
echo "The total number of parameters passed is $#"
Now execute the script "test-param.sh"
$ chmod u+x test-param.sh
$ ./test-param.sh p1 p2 p3 p4 p5 p6 p7 p8 p9
The script file is ./test-param.sh
The first parameter passed to ./test-param.sh is p1 and the second parameter is p2
p1 p2 p3 p4 p5 p6 p7 p8 p9 are the parameters passed to ./test-param.sh
The total number of parameters passed is 9
I hope you have understand what those variables are used for.
$0 - To get the base name (script file name).
$1 to $9 - Will give the parameters (or arguments) passed to a script during execution.
$@ - Will display all parameters passed to a script.
$* - Will also display all parameters passed to a script. But they are not separated by space. (p1p2p3 etc)
@# - Will give the total number of parameters passed to a script.
TIP
What if there are more that 9 parameters. How to display a 10th parameter. Answer is use, ${10}
Now $?, called as "exit status". In linux when a command is executed without any errors, it produce an exit status, 0. ie, $? will be 0. A value other than 0 is considered as an error. Technically, $? is exit status of the most recently executed foreground pipeline.
Lets illustrate this,
$ ls anil.txt
If ls can list anil.txt, the exit status ($?) will be "0". If ls can't list anil.txt exit status will be a value not equal to 0.
Another example.
$ echo "Linux rocks" | grep -q windows (-q will suppress grep output)
$ echo $?
1
Why? Because grep can't find windows in"Linux rocks". In general, if the last command doesn't produce the desired output (an error) the exit status ($?) will be a value greater than "0". Zero means success.
Some more inbuilt variables
$$ - Priocess ID of the shell. If used inside a script $$ will get process id of the script.
$! - Process ID of last executed background process.
When using for loop you may sometimes want to loop through a ":" separated or "-" separated fields. How will you do that. By setting inbuilt variable IFS (Internal field separator). See the following example to understand this.
$ IFS="-"
$ for i in `echo that-is-cool`; do echo $i; donethat
is
cool
If IFS is not set to "-" the output of for loop will be. Because the default value of IFS is space " "
that-is-cool
Always remember to unset IFS, after its use.
unset IFS
More to come . . .
Subscribe to my feeds . . . SUBSCRIBE
Please post your comments on this. If possible your on suggestions and tips.
Thanks.
November 15th, 2007 at 8:14 am
[…] read more | digg story […]
May 8th, 2008 at 1:34 am
Nice post - very easy to read and understand.
Just wanted to note that this built-in ( $- ) comes in handy for me a lot. It returns all the flags that the shell was invoked with, like:
-bash-3.2$ echo $-
himBH
Basically, that’s an interactive shell (i). I find this very helpful in doing checking within .profile or .bashrc files. That way if the shell is invoked interactively, I can set extra variables that wouldn’t any sense to add to a non-interactive login, like an ssh command run remotely.
Great post, again!
Thanks,
, Mike