Magazine

Command-line Arguments in Linux Shell

Posted on the 28 March 2021 by Satish Kumar @satish_kumar86

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

Ifmy_commandis 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 parameters19

${10}

Positional parameter10

$#

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 notpassarguments to thecommand line, but we may need to set parameters internally inside the script.

We can declare parameters with thesetcommand, 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$2to extractthemonth fromtheoutput.

Let’s write scriptset_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$3do not contain any information.
  • Then, we set$1to$3asGERMANnumerals in words.
  • Then, we set$1to$5as the name, phone number, address, date of birth, and salary, respectively.

Understanding shift

Usingshift, we can changetheparameter to which$1and$2are pointing tothenext variable.

Create scriptshift_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,$1wasOne. After the shift,$1will be pointing toTwo. Once the shift has been done, the value in position1is always destroyed and is inaccessible.

Create scriptshift_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,$1to$13were numerical values1to13, respectively.
  • When we called the command shift, it then$1shifted to number2, and all$numberswere shifted accordingly.
  • When we calledthecommand shift2, then$1shiftedtonumber4and all$numberswere shifted accordingly.

Resetting positional parameters

In certain situations, we may needtoreset original positional parameters.

Let’s try the following:

$ set Alan John Dennis

This will reset the positional parameters.

Now$1isAlan,$2isJohn, and$3isDennis.

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

Back to Featured Articles on Logo Paperblog