Command-line arguments in Linux shell

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:

$  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 parameters:

$0

Shell-script name or command

$1-$9

Positional parameters 19

${10}

Positional parameter 10

$#

Total number of parameters

$*

Evaluates for all the positional parameters

$@

Same as $*, except when double quoted

"$*"

Displays all parameters as "$1 $2 $3", and so on

"$@"

Displays all parameters as "$1" "$2" "$3", and so on

Let’s create a script parameter.sh, as follows:

parameter.sh

#!/bin/bash 
echo "Total number of parameters are = $#" 
echo "Script name = $0" 
echo "First Parameter is $1" 
echo "Second Parameter is $2" 
echo "Third Parameter is $3" 
echo "Fourth Parameter is $4" 
echo "Fifth Parameter is $5" 
echo "All parameters are = $*"

Then, as usual, give execute permission to the script and then execute it:

$ ./parameter.sh London Washington Delhi Dhaka Paris

The output is as follows:

Output:

Total number of parameters are = 5Command is = ./parameter.shFirst Parameter is LondonSecond Parameter is WashingtonThird Parameter is DelhiFourth Parameter is DhakaFifth Parameter is ParisAll parameters are = London Washington Delhi Dhaka Paris

Understanding set

Many times, we may not pass arguments to the command line, but we may need to set parameters internally inside the script.

We can declare parameters with the set command, as follows:

$ set USA Canada UK France
$ echo $1USA
$ echo $2Canada
$ echo $3UK
$ echo $4France

We can use this inside the set_01.sh script, as follows:

set_01.sh

#!/bin/bash 
set USA Canada UK France 
echo $1 
echo $2 
echo $3 
echo $4

Run the script as this:

$ ./set.sh

The output is as follows:

Output:

USACanadaUKFrance

Following is a summary of the declare options:

Option

Meaning

-a

An array is created

-f

Displays the function names and definitions

-F

Displays only the function names

-i

Makes the variables integer types

-r

Makes the variables read-only

-x

Exports the variables

Type in the following commands:

set One Two Three Four Five
echo $0      # This will show command
echo $1       # This will show first parameter
echo $2echo $*    # This will list all parameters
echo $#    # This will list total number of parameters
echo ${10} ${11}  # Use this syntax for parameters for 10th and  # 11th parameters

Let’s write script set_02.sh, as follows:

set_02.sh

#!/bin/bash 
echo The date is $(date) 
set $(date) 
echo The month is $2 
exit 0

The output is as follows:

Output:

$ bash set_03.sh
Executing script set_03.sh

One two three in German are:
eins
zwei
drei
name phone address birthdate salary
At this time $1 = name and $4 = birthdate

In the script $(date), the command will execute, and the output of that command will be used as $1, $2, $3, and so on. We have used $2 to extract the month from the output.

Let’s write script set_03.sh, as follows:

set_03.sh

#!/bin/bash 
 
echo "Executing script $0" 
echo $1 $2 $3 
 
set eins zwei drei 
echo "One two three in German are:" 
echo "$1" 
echo  "$2" 
echo  "$3" 
 
textline="name phone address birthdate salary" 
set $textline 
echo  "$*" 
echo 'At this time $1 = '$1' and $4 = '$4''

The output is as follows:

Output:

Executing script ./hello.sh 
 
One two three in German are: 
eins 
zwei 
drei 
 
name phone address birthdate salary 
At this time $1 = name and $4 = birthdate

Output:

$ bash shift_02.sh  1 2 3 4 5 6 7 8 9 10 11 12 13
$#: 13
$@: 1 2 3 4 5 6 7 8 9 10 11 12 13
$*: 1 2 3 4 5 6 7 8 9 10 11 12 13

$1 $2 $9 $10 are: 1 2 9 10
$#: 12
$@: 2 3 4 5 6 7 8 9 10 11 12 13
$*: 2 3 4 5 6 7 8 9 10 11 12 13

$1 $2 $9 are: 2 3 10
$#: 10
$@: 4 5 6 7 8 9 10 11 12 13
$*: 4 5 6 7 8 9 10 11 12 13

$1 $2 $9 are: 4 5 12
${10}: 13

In this script, the output shows:

  • Initially, when the set is not called, then $1, $2, and $3 do not contain any information.
  • Then, we set $1 to $3 as GERMAN numerals in words.
  • Then, we set $1 to $5 as the name, phone number, address, date of birth, and salary, respectively.

Understanding shift

Using shift, we can change the parameter to which $1 and $2 are pointing to the next variable.

Create script shift_01.sh, as follows:

shift_01.sh

#!/bin/bash 
echo "All Arguments Passed are as follow : " 
echo $* 
echo "Shift By one Position :" 
shift 
echo "Value of Positional Parameter $ 1 after shift :" 
echo $1 
echo "Shift by Two Positions :" 
shift 2 
echo "Value of Positional Parameter $ 1 After two Shifts :" 
echo $1

Execute the following command:

$ chmod +x shift_01.sh
$ ./shift_01.sh One Two Three Four

The output is as follows:

Output:

$ ./shift_01.sh One Two Three Four
All arguments passed are as follows:
One Two Three Four
Shift by one position.
Here, the value of the positional parameter $1 after shift is:
Two
Shift by two positions.
The value of the positional parameter $1 after two shifts:
Four

We can see that, initially, $1 was One. After the shift, $1 will be pointing to Two. Once the shift has been done, the value in position 1 is always destroyed and is inaccessible.

Create script shift_02.sh, as follows:

shift_02.sh

#!/bin/bash 
 
echo '$#: ' $# 
echo '$@: ' $@ 
echo '$*: ' $* 
echo 
echo '$1 $2 $9 $10 are: ' $1 $2 $9 $10 
echo 
 
shift 
echo '$#: ' $# 
echo '$@: ' $@ 
echo '$*: ' $* 
echo 
echo '$1 $2 $9 are: ' $1 $2 $9 
 
shift 2 
echo '$#: ' $# 
echo '$@: ' $@ 
echo '$*: ' $* 
echo 
echo '$1 $2 $9 are: ' $1 $2 $9 
 
echo '${10}: ' ${10}

Output:

$ bash shift_02.sh  1 2 3 4 5 6 7 8 9 10 11 12 13
$#: 13
$@: 1 2 3 4 5 6 7 8 9 10 11 12 13
$*: 1 2 3 4 5 6 7 8 9 10 11 12 13

$1 $2 $9 $10 are: 1 2 9 10
$#: 12
$@: 2 3 4 5 6 7 8 9 10 11 12 13
$*: 2 3 4 5 6 7 8 9 10 11 12 13

$1 $2 $9 are: 2 3 10
$#: 10
$@: 4 5 6 7 8 9 10 11 12 13
$*: 4 5 6 7 8 9 10 11 12 13

$1 $2 $9 are: 4 5 12
${10}: 13

From this script’s execution, the following output is shown:

  • Initially, $1 to $13 were numerical values 1 to 13, respectively.
  • When we called the command shift, it then$1 shifted to number 2, and all $numbers were shifted accordingly.
  • When we called the command shift 2, then $1 shifted to number 4 and all $numbers were shifted accordingly.

Resetting positional parameters

In certain situations, we may need to reset original positional parameters.

Let’s try the following:

$ set Alan John Dennis

This will reset the positional parameters.

Now $1 is Alan, $2 is John, and $3 is Dennis.

Inside scripts, we can save positional parameters in a variable, as follows:

oldargs=$*

Then, we can set new positional parameters.

In addition, we can bring back our original positional parameters, as follows:

set $oldargs

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

twenty − 2 =