Passing Arguments or Variables to a Bash Script from the Command Line

November 17, 2009

Often when working with Bash scripts we find that we want to add some options to the script by way of arguments or variables. Easily enough Bash command line arguments can be accessed by $1, $2, etc within the script where $1 is the first argument or variable and #2 is the second etc.

Example:

#!/usr/bin/env bash
echo the first argument is $1
echo second argument is $2
echo eleventh argument is $11
echo total number of arguments is $#