Bash Command Line Arguments in Shell Scripts
Bash Command Line Arguments are values supplied to a bash shell script at the time of execution. They let the same script run with different input values without asking the user for interactive input inside the script.
For example, if a script is executed as ./backup.sh /home/arjun daily, the script receives /home/arjun as the first command line argument and daily as the second argument. Inside the script, these values are available through special bash variables called positional parameters.
Bash positional parameters for command line arguments
In bash shell programming, command line arguments are accessed as $1, $2, $3, and so on. $1 refers to the first argument, $2 refers to the second argument, and $3 refers to the third argument. The script name itself is available as $0.
Older shell examples often mention only nine arguments because $1 through $9 can be written directly. In Bash, you can read arguments beyond the ninth argument by using braces, such as ${10}, ${11}, or ${21}. Braces remove ambiguity and make it clear which positional parameter you want.
| Bash parameter | Meaning inside a script | Example use |
|---|---|---|
$0 | Name or path used to run the script | ./script.sh |
$1 | First command line argument | "$1" |
$2 | Second command line argument | "$2" |
${10} | Tenth command line argument | "${10}" |
$# | Total number of arguments passed | if [[ $# -lt 2 ]] |
"$@" | All arguments, preserved as separate values | for arg in "$@" |
"$*" | All arguments combined using the first character of IFS | Less common for safe argument handling |
Example 1 – Read Bash command line arguments by position
In this example we will read four arguments from command line, and use them in our script.
Bash Script
#!/bin/bash
echo "Hello $1 !"
echo "We like your place, $2."
echo "Thank you for showing interest to learn $3 : $4."
Output
arjun@arjun-VPCEH26EN:~/ws$ ./bash-command-line-arguments Arjun India Bash-Tutorial Command-Line-Arguments
Hello Arjun !
We like your place, India.
Thank you for showing interest to learn Bash-Tutorial : Command-Line-Arguments.
Note : If you are wondering what $0 would be, it is the shell script name you provide.
When a command line argument may contain spaces, quote it while running the script and quote the variable while using it inside the script. For example, run ./script.sh "New Delhi" and use "$1" inside the script. Quoting prevents the value from being split into multiple words.
Recommended Bash syntax for quoted command line arguments
The following syntax shows the safer habit of quoting positional parameters. This is useful when file paths, names, messages, or other arguments may contain spaces or special characters.
echo "First argument: $1"
echo "Second argument: $2"
For a script that expects a file path and a label, a practical version would look like this.
#!/bin/bash
file_path="$1"
label="$2"
echo "File path: $file_path"
echo "Label: $label"
./show-file.sh "/home/arjun/My Documents/report.txt" "Monthly report"
File path: /home/arjun/My Documents/report.txt
Label: Monthly report
Example 2 – Count command line arguments with $#
Following is a Bash shell script to print total number of command line arguments passed to shell script.
Bash Script
#!/bin/bash
echo "$# arguments have been passed to this script, $0"
Output
arjun@arjun-VPCEH26EN:~/ws$ ./bash-command-line-arguments-2 Arjun India Bash-Tutorial Command-Line-Arguments as as s s s s s s s s s s s
17 arguments have been passed to this script, ./bash-command-line-arguments-2
$# is commonly used for argument validation. If a script requires two arguments, check the count before using $1 and $2. This gives the user a clear error message instead of letting the script fail later.
#!/bin/bash
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <source-file> <destination-file>"
exit 1
fi
source_file="$1"
destination_file="$2"
echo "Copying from $source_file to $destination_file"
Usage: ./copy-file.sh <source-file> <destination-file>
Example 3 – Access Bash arguments after the ninth position
In the following example, we will write a bash shell script that uses more than nine arguments.
Curly braces are used like ${nn} , to access higher arguments (>9)
Bash Script
#!/bin/bash
echo "13th argument is ${13}"
Output
arjun@arjun-VPCEH26EN:~/ws$ ./bash-command-line-arguments-3 Bash-Tutorial Command-Line-Arguments a b c d e f g h i j k 5 27 9
13th argument is k
Do not write $10 when you mean the tenth argument. Bash reads $10 as $1 followed by the character 0. Use ${10} for the tenth argument and the same brace style for any multi-digit positional parameter.
echo "Tenth argument: ${10}"
echo "Eleventh argument: ${11}"
Loop through all Bash command line arguments with “$@”
Use "$@" when a script must process every argument. Quoting "$@" preserves each argument as a separate value, even when an argument contains spaces.
#!/bin/bash
counter=1
for argument in "$@"; do
echo "Argument $counter: $argument"
((counter++))
done
./print-arguments.sh apple "green mango" banana
Argument 1: apple
Argument 2: green mango
Argument 3: banana
In most scripts, prefer "$@" over $*. The quoted "$@" form is the safest way to forward or loop over arguments without accidentally joining or splitting them.
Use shift to process Bash arguments one by one
The shift command moves positional parameters to the left. After shift, the old $2 becomes the new $1. This is useful for scripts that process arguments in order.
#!/bin/bash
while [[ $# -gt 0 ]]; do
echo "Current argument: $1"
shift
done
./read-all.sh one two three
Current argument: one
Current argument: two
Current argument: three
Parse Bash options such as -f and -v with getopts
Positional arguments are enough for simple scripts, but many command line tools also support options such as -v for verbose mode or -f file.txt for an input file. Bash provides getopts for parsing short options in a predictable way.
#!/bin/bash
verbose=false
file=""
while getopts ":vf:" option; do
case "$option" in
v)
verbose=true
;;
f)
file="$OPTARG"
;;
:)
echo "Option -$OPTARG requires a value."
exit 1
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
echo "Verbose mode: $verbose"
echo "File: $file"
echo "Remaining arguments: $*"
./process.sh -v -f report.txt extra1 extra2
Verbose mode: true
File: report.txt
Remaining arguments: extra1 extra2
Use getopts when your script accepts named options. Use plain positional parameters when the order itself is clear, such as ./resize.sh input.jpg output.jpg.
Common Bash command line argument mistakes
- Not quoting arguments: Use
"$1"and"$@"when arguments may contain spaces, wildcards, or special characters. - Using
$10for the tenth argument: Use${10}instead. - Skipping argument count checks: Validate
$#before using required arguments. - Confusing options and arguments: Options usually start with
-, while positional arguments depend on order. - Using
$*when"$@"is needed:"$@"keeps arguments separate and is usually safer.
Bash command line arguments QA checklist
- The tutorial explains
$0,$1,$#,"$@", and${10}clearly. - The examples quote positional parameters where spaces or special characters may appear.
- The script validates the number of required arguments before using them.
- The difference between positional arguments and options is shown with a Bash-specific example.
- The examples avoid unsafe assumptions such as a strict nine-argument Bash limit.
- Output blocks show only terminal results and new command blocks use
language-bash.
FAQs on Bash command line arguments
How do I pass command line arguments to a Bash script?
Write the values after the script name when running it. For example, ./script.sh apple banana passes apple as $1 and banana as $2.
How do I access the tenth argument in Bash?
Use braces: ${10}. Do not use $10, because Bash interprets that as $1 followed by the character 0.
How do I count arguments in a Bash script?
Use $#. For example, if [[ $# -ne 2 ]] checks whether exactly two command line arguments were passed.
What is the difference between “$@” and “$*” in Bash?
Quoted "$@" expands to all arguments while preserving each argument as a separate value. Quoted "$*" combines the arguments into one value. For loops and forwarding arguments, "$@" is usually preferred.
When should I use getopts in a Bash script?
Use getopts when your script accepts short options such as -v or -f filename. For simple ordered input values, positional parameters such as $1 and $2 are usually enough.
Summary: using command line arguments in Bash scripts
In this Bash Tutorial – Bash Command Line Arguments, we have learnt to read and use command line arguments inside bash shell scripts. Use $1, $2, and other positional parameters for ordered input, $# to validate the argument count, ${10} for arguments beyond the ninth position, and "$@" when looping through all arguments safely.
TutorialKart.com