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:


Shell script ...

Get Learning Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.