What are command line arguments
Command line arguments are the values that are supplied to a shell script as input while executing it from the command line. Using command line arguments your script can read values directly from command line or terminal.
Command line arguments are also called positional parameters.
Command line arguments make a shell script dynamic and maintainable in that you can execute a shell script with different values supplied from outside and you do not need to write them inside the script.
This means that for each set of values, you do not need to go and change the script.
Passing arguments to shell script
A shell script may be provided single or multiple input arguments. These arguments are written after the name of the script and each argument is separated by a space. Example,
Above command will execute a shell script along with 3 command line parameters.
If a single command line arguments consists of multiple words, then it should be enclosed between quotes as
These are two command line arguments.
Accessing command line arguments in shell script
In the last section, you saw how to supply command line arguments to a shell script. This section will explain how to use them in a shell script.
Command line arguments are accessed using numeric indexes inside a shell script preceded with a dollar($
) sign. That is, first argument can be referred as $1
, second as $2
and so on. Example,
#! /bin/bash
echo “Displaying top $2 lines of file $1”
head –$2 $1
This shell script reads a file whose name is supplied as a command line argument and displays its lines from the top where first argument is the file name and second is the number of lines to display.
Notice the use of $1
and $2
to refer to the command line arguments where $1
refers to the first argument and $2
refers to the second.
head
command is used for reading the contents of a file.
This shell script should be executed as
where the first argument is file name and second is the number of lines. Its output will be
This is a sample file
just for demo purpose.
Look, how $1
is replaced with the name of file and $2
replaced with the number of lines.
If the number of arguments is lesser than that referred inside the shell script, then there will be no error but an empty value will be substituted in place of missing argument.
Counting the number of command line arguments
Many times you need to check if a fixed number of arguments have been supplied or not before executing the shell script. Example, when reading a file, you need to ensure that at least 1 argument has been given.
Shell script provides a $#(dollar followed by pound symbol) operator which returns the number of command line arguments supplied to the script. Example,
#! /bin/bash
# store the number of arguments in a variable
count=$#
echo “Number of command line arguments: $count”
# check if file name is supplied
if [ $count != 0 ];then
echo “Displaying top 10 lines of file $1”
head -10 $1
else
echo “Invalid file name”
fi
Above script checks if at least 1 argument is supplied and only then, it reads the file otherwise displays an error message.
Two different invocations of this shell script are
Number of command line arguments: 1
Displaying top 2 lines of file data.properties
This is a sample file
just for demo purpose.
./reader
Number of command line arguments: 0
Invalid file name
Getting all command line arguments
$@ operator provides all the input arguments that are supplied to the shell script. Example,
#! /bin/bash
echo “Input arguments are: $@”
Output of above script is
Input arguments are: This is command line 123
See, all input arguments are printed as they were supplied to the shell script.
Iterate over command line arguments
Suppose you have to write a shell script that will accepts multiple file paths as command line arguments and delete each file.
You can access input arguments as $1
, $2
etc., but you do not know how many arguments have been supplied. Solution is to loop over all the arguments(or file paths) and delete them one by one.
As stated in the last section, $@ provides all the command line arguments. We can use this to iterate over them. Example,
#! /bin/bash
for filename in “$@”;do
echo “Deleting file $filename”
rm $filename
done
Output of execution is
Deleting file dummy.txt
Deleting file demo.sh
Deleting file sample.doc
Shifting arguments
Command line arguments are referred using indexes. It is also possible to move these indexes such that the first index refers to some other argument such as second argument.
This is done using shift
operator. This operator is followed by an integer which is the number of places that you want to shift the arguments.
Thus, shift 2
means that arguments will be shifted 2 places to the left meaning that $1
will now refer to the third argument, $2
to the fourth argument and so on. Example,
#!bin/bash
shift 2
echo “First argument: $1”
echo “Second argument: $2”
echo “Third argument: $3”
Output will be
Second argument: 5
Third argument: 7
Arguments have shifted 2 places to left so that $1
will hold the third argument and so on.
Remember that it is not necessary to write shift at the beginning of script. It can be written anywhere. Example,
#! /bin/bash
echo “First argument: $1”
shift 2
echo “Second argument: $2”
echo “Third argument: $3”
Output now becomes
First argument: 1
Second argument: 5
Third argument: 7
shift
is written after the first argument has been read and then argument locations are shifted by 2 places which means that fourth argument will now be referred by $2
and so on.
To summarize, refer below pointers
- Command line arguments to a shell script can be referred as
$1
,$2
etc., in the order in which they are supplied. - Count of arguments can be retrieved using $# operator.
- All input arguments are fetched using $@ operator.
- Input arguments can be shifted so that
$1
may be made to an input argument other than first one usingshift
operator. shift
is followed by a number which is the number of places that the command line arguments need to be shifted.
If you liked the article, do not forget to click the clap icon below.