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 they command which file or group of files to process (reading/writing of files)
  • Command-line arguments tell the command/utility which option to use

Check out the following command line:

[student@localhost ~]$  my_program  arg1  arg2  arg3

If my_command is a bash shell script, then we can access every command-line positional parameter 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 a summary of the positional ...

Get Learning Linux Shell Scripting - Second Edition 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.