[Unix] Command Line Arguments

Shell - Command Line Arguments

Command line arguments are special variables that can be changed during run time.  The command line arguments are enumerated in the following manner $0 is the name of the command

$1 first parameter
$2 second parameter
$3 third parameter and so on..

To reference variables after the ninth argument you must enclose the number in brackets like this ${nn}.

As well as the commandline arguments there are some special builtin variables. They are described as follows:
•$# represents the count of total number of parameters. Useful for controlling loop constructs that need to process each parameter.
•$@ / $* expands to all the parameters separated by spaces. Useful for passing all the parameters to some other function or program.
•$- expands to the flags(options) the shell was invoked with. Useful for controlling program flow based on the flags set.
•$$ expands to the process id of the shell innovated to run the script. Useful for creating unique temporary filenames relative to this instantiation of the script
•$0 is the name of the command

Sample script to understand Command Line Arguments

# Call this script with at least 3 parameters, for example
# sh scriptname 1 2 3
echo "first parameter is $1"
echo "Second parameter is $2"
echo "Third parameter is $3"
echo "Total Number of parameters is $#"
echo "List of all parameters is $*"
exit 0

Hope this helps !!