Bash Command line Arguments

These are variables that contain the arguments to a script when it is run. These variables are accessed using $1, $2, … $n, where $1 is the first command-line argument, $2 the second, etc. Arguments are delimited by spaces. $0 is the name of the script. The variable $# will display the number of command-line arguments supplied; this number is limited to 9 arguments in the older shells, and is practically unlimited in the modern ones.

Example:

Consider a script that will take two command-line arguments and display them.

#!/bin/sh
echo “The first variable is $1″
echo “The second variable is $2

Comments are closed.