Working with command line arguments (special variables, set and shift, getopt)
Command line arguments are required for the following reasons:
They inform the utility or command as to which file or group of files to process (reading/writing of files)
Command line arguments tell the command/utility which option to use
Check the following command line:
student@ubuntu:~$ my_program arg1 arg2 arg3
If my_command
is a bash Shell script, then we can access every command line positional parameters inside the script as follows:
$0 would contain "my_program" # Command $1 would contain "arg1" # First parameter $2 would contain "arg2" # Second parameter $3 would contain "arg3" # Third parameter
The following is the summary of positional parameters:
$0 |
Shell script name or command |
$1–$9 |
Positional parameters 1–9 |
${10} |
Positional parameter 10 |
$# |
Total number of parameters |
$* |
Evaluates to all the positional parameters |
$@ |
Same as $*, except when double quoted |
"$*" |
Displays... |