Search This Blog

Thursday, June 21, 2007

Special shell variables in bash

Bash defines some special variables which are useful while writing shell scripts. They are

$0 = Filename of script
$1 = Positional parameter #1
$2 - $9 = Positional parameters #2 - #9
${10} = Positional parameter #10
$# = Number of positional parameters
"$*" = All the positional parameters (as a single word)[*]
"$@" = All the positional parameters (as separate stings)
${#*} = Number of command line parameters passed to script
${#@} = Number of command line parameters passed to script
$? = Return value
$$ = Process ID (PID) of the script
$- = Flags passed to script (using set)
$_ = Last argument of previous command
$! = Process ID (PID) of last job run in background

[*] Must be quoted, otherwise it defaults to "$@"

Reference :- Appendix B of "Advanced Bash-Scripting guide" by Mendel Cooper, Version 5.0. Debian users can get this document by installing the abs-guide package.

sudo apt-get install abs-guide

The necessary files can be found in /usr/share/doc/abs-guide . However, the abs-guide package contains only the html version of the document. A pdf version can be downloaded from http://www.tldp.org/LDP/abs/abs-guide.pdf .

No comments:

Followers