Skip to content
Related Articles

Related Articles

Bash Script – How to use Command Line Arguments

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Feb, 2022
View Discussion
Improve Article
Save Article

In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part.

Positional Parameters

Command-line arguments are passed in the positional way i.e. in the same way how they are given in the program execution.  Let us see with an example. 

Create a shell program that can display the command line arguments in a positional way. “Nano” editor is used to create the shell program”

Nano editor is used to coding the shell script

The program can be executed by using the “sh” command.

sh <script filename> arg1 arg2 arg3 ......

So, our code of execution will become

sh displayPositionalArgument.sh Welcome To GeeksForGeeks

Diagrammatic representation of the above code:

Diagrammatic representation

Always the first argument starts after the <script filename>. <script filename> will be in the 0th location, We need to take positional arguments after the <script filename>. Hence in the above example

$1-> "Welcome"
$2-> "To"
$3-> "GeeksForFeeks"

Note: We can pass n number of arguments and they are identified by means of their position.

The output of the above code :

Output

Some special variables are also to be noted while handling command-line arguments.

Special Variable Special Variable’s details
$1 … $n Positional argument indicating from 1 .. n. If the argument is like 10, 11 onwards, it has to be indicated as ${10},${11
$0 This is not taken into the argument list as this indicates the “name” of the shell program. In the above example, $0 is “displayPositionalArgument.sh”
$@ Values of the arguments that are passed in the program. This will be much helpful if we are not sure about the number of arguments that got passed.
$# Total number of arguments and it is a good approach for loop concepts.
$* In order to get all the arguments as double-quoted, we can follow this way
$$ To know about the process id of the current shell
$? and $! Exit status id and Process id of the last command

Using Flags

Arguments can be passed along with the flags. The arguments can be identified with a single letter having – before that. A single letter can be meaningful and here let us take -1, -2, and -3.

We need to use getopts function to read the flags in the input, and OPTARG refers to the corresponding values:

Program2 using flags

The above program can be executed as

sh usingFlags.sh -1 ‘GeeksForGeeks’ -2 ‘JavaProgramming’ -3 100

Output

Output

Using Loops with $@ – Loop Constructs

Though positional parameters help to send as command-line arguments, if there are more arguments, we cannot count and use them. Instead by using $@, we can achieve that. It is nothing but the array of all the parameters passed. Iterating over a for loop, we can get that.

Program3 that contains $@

Output :

Another way is by using the Shift operator instead of $@ – Shift operator:

The $# variable is used to return the input size. By  using that and with the shift operator we can achieve instead of $@

Program4

Output:

General Use case scenario using Shift operator:

Assume that in the command arguments, we may pass the “-d” argument which is nothing but to know it is a directory. 

  • If there is -d is given, then we need to pick the next argument and check is it a directory like that.
  • If there is no -d, then check whether is it a file or not

Then display accordingly as to whether the argument is a directory or file

In the comments, the flow is defined

Let the name of the script be “shiftArgument.sh”

sh  shiftArgument.sh -d /home shiftArgument.sh 

Output:

Output

The above code will check “/home” as a directory as we have “-d” as the first positional argument and hence shift operator is done and the next argument is checked against to see is it a directory. If there is no “-d” argument as the prior one, it just checks “whether that argument is a file or not”

Conclusion

We can pass command-line arguments at our convenience in a positional way or with flags. Moreover useful concepts like $@ (array of all the parameters), $# (by calculating input size)  are available and with that, we can iterate in the  “for” or “while” loops, we can access the arguments.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!