Bash Command Line Arguments

Bash Command Line Arguments

Bash Command Line Arguments are used to provide input to a bash shell script while executing the script.

In bash shell programming you can provide maximum of nine arguments to a shell script. Inside the shell script you can access the arguments as bash variables $1 , $2 , $3 … corresponding to first argument, second argument, third argument etc., respectively.  For multiple digit arguments use curly braces as${21}  for 21st argument. But it is recommended that you limit your command line arguments to nine for maintaining compatibility to other shells and avoiding confusion.


Example Shell Script to demonstrate Bash Command Line Arguments

Following is an example that reads four arguments from command line :

Note : If you are wondering what $0 would be, it is the shell script name you provide.


Example bash shell script to print total number of command line arguments passed to shell script


Example bash shell script that uses more than nine arguments

Curly braces are used like ${nn} , to access higher arguments (>9)


Conclusion :

In this Bash Tutorial – Bash Command Line Arguments, we have learnt to read and use command line arguments inside bash shell scripts.