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 ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.