So far, we have seen the usage of the commands such as grep
, head
, ls
, cat
, and many more. These commands also support passing arguments to a command via a command line. Some of command line arguments are input files, output files, and options. Arguments are provided as per output needs. For example, ls -l filename
is executed to get a long listing output, while ls -R filename
is used to display recursively the contents of a directory.
Shell script also supports providing command line arguments that we can process further by a shell script.
The command line arguments can be given as follows:
<script_file> arg1 arg2 arg3 … argN
Here, script_file
is a shell script file to be executed, and arg1
, arg2
, arg3
, argN
, and so on, are command line parameters.
Command line arguments are passed to a shell script as positional parameters. So, arg1
will be accessed in a script as $1
, arg2
as $2
, and so on.
The following shell demonstrates the usage of the command line arguments:
#!/bin/bash # Filename: command_line_arg.sh # Description: Accessing command line parameters in shell script # Printing first, second and third command line parameters" echo "First command line parameter = $1" echo "Second command line parameter = $2" echo "Third command line parameter = $3"
The following output is obtained after running the command_line_arg.sh
script with arguments:
$ sh command_line_arg.sh Linux Shell Scripting First command line parameter = Linux Second command line parameter = Shell Third command line parameter = Scripting
The following table shows special variables that are useful to get more information about command line parameters:
Special variables |
Description |
---|---|
|
Number of the command line arguments |
|
Complete set of command line arguments in a single string—that is, |
|
Complete set of command line arguments, but each argument is enclosed in separate quotes—that is, |
|
Name of the shell script itself |
|
Refers to argument1, argument2, …, argumentN, respectively |
Using $#
in a script to check the number of command line arguments will be very helpful to process arguments further.
The following is another shell script example that takes command line arguments:
#!/bin/bash # Filename: command_line_arg2.sh # Description: Creating directories in /tmp # Check if at least 1 argument is passed in command line if [ $# -lt 1 ] then echo "Specify minimum one argument to create directory" exit 1 else pushd /tmp > /dev/null echo "Directory to be created are: $@" mkdir $@ # Accessing all command line arguments fi
The following output is obtained after executing the command_line_arg2.sh
script:
$ sh command_line_arg2.sh a b Directory to be created are: a b $ sh command_line_arg2.sh Specify minimum one argument to create directory
To shift command line arguments towards the left, the shift
built in can be used. The syntax is as follows:
shift N
Here, N
is the number of arguments by which it can shift to the left.
For example, suppose the current command line arguments are arg1
, arg2
, arg3
, arg4
and arg5
. They can be accessed in a shell script as $1
, $2
, $3
, $4
, and $5
, respectively; the $#
value is 5
. When we call shift 3
, arguments get shifted by 3
. Now, $1
contains arg4
and $2
contains arg5
. Also, the $#
value is now 2
.
The following shell script demonstrates the usage of shift
:
#!/bin/bash # Filename: shift_argument.sh # Description: Usage of shift shell builtin echo "Length of command line arguments = $#" echo "Arguments are:" echo "$1 = $1, $2 = $2, $3 = $3, $4 = $4, $5 = $5, $6 = $6" echo "Shifting arguments by 3" shift 3 echo "Length of command line arguments after 3 shift = $#" echo "Arguments after 3 shifts are" echo "$1 = $1, $2 = $2, $3 = $3, $4 = $4, $5 = $5, $6 = $6"
The following output is obtained after running the shift_argument.sh
script with the arguments a b c d e f
:
$ sh shift_argument.sh a b c d e f Length of command line arguments = 6 Arguments are: $1 = a, $2 = b, $3 = c, $4 = d, $5 = e, $6 = f Shifting arguments by 3 Length of command line arguments after 3 shift = 3 Arguments after 3 shifts are $1 = d, $2 = e, $3 = f, $4 = , $5 = , $6 =
Providing command line options make shell scripts more interactive. From the command line arguments, we can also parse options for further processing by a shell script.
The following shell script shows the command line usage with options:
#!/bin/bash # Filename: myprint.sh # Description: Showing how to create command line options in shell script function display_help() { echo "Usage: myprint [OPTIONS] [arg ...]" echo "--help Display help" echo "--version Display version of script" echo "--print Print arguments" } function display_version() { echo "Version of shell script application is 0.1" } function myprint() { echo "Arguments are: $*" } # Parsing command line arguments if [ "$1" != "" ] then case $1 in --help ) display_help exit 1 ;; --version ) display_version exit 1 ;; --print ) shift myprint $@ exit 1 ;; *) display_help exit 1 esac fi
The following output is obtained after executing the myprint.sh
script:
$ sh myprint.sh --help Usage: myprint [OPTIONS] [arg ...] --help Display help --version Display version of script --print Print arguments $ sh myprint.sh --version Version of shell script application is 0.1 $ sh myprint.sh --print Linux Shell Scripting Arguments are: Linux Shell Scripting
3.136.22.192