Friday, October 9, 2009

Linux - Bash Shell Scripting - Command-Line Arguments

When a Bash shell script is launched, arguments may be passed to the script, e.g., as in $ ./mymake.sh foo bar install. Here mymake.sh is the name of the shell script file, and foo, bar, and install are arguments. Special shell variables, $0, $1, $2, …, are used to refer to the command line arguments. The variable $0 refers to the name of the shell script (./mymake.sh), $1 the first argument (foo), $2 the second argument (bar) and so on. $# is the number of command line arguments following the script filename. Our script above can be rewritten,

#!/bin/bash
# usage: rmwild dir pattern
dirname=$1
pattern=$2
cd $dirname
rm $pattern

No comments:

Post a Comment