Bash Arguments
Command line arguments are values typed after the script name. They let one script handle many different jobs without any code change. The command cp file1.txt backup/ works the same way: cp is the program, and the two words after it are arguments.
How Arguments Reach a Script
Bash stores each argument in a numbered variable. The number shows the position.
./greet.sh Asha Pune Blue
| | | |
| | | +--> $3
| | +---------> $2
| +----------------> $1
+-------------------------> $0 (script name)
Special Argument Variables
| Variable | Meaning |
|---|---|
| $0 | Name of the script |
| $1 to $9 | First to ninth argument |
| ${10} | Tenth argument and beyond need braces |
| $# | Count of arguments |
| $@ | All arguments as separate words |
| $* | All arguments as one single string |
| $? | Exit status of the last command |
Read Arguments in a Script
Create a file named greet.sh:
#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Total arguments: $#"Run it with two values:
./greet.sh Asha PuneOutput:
Script name: ./greet.sh
First argument: Asha
Second argument: Pune
Total arguments: 2Loop Over Every Argument
The $@ variable suits any number of values:
#!/bin/bash
for item in "$@"
do
echo "Processing: $item"
doneRun ./list.sh apple "red grape" mango and the loop treats red grape as one item, because the quotation marks keep the words together.
Check for Missing Arguments
A good script warns the user when a required value is absent:
#!/bin/bash
if [ $# -lt 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
echo "Reading $1"The exit 1 line stops the script and reports a failure.
Use the shift Command
The shift command drops the first argument and moves the others one place left. Scripts use it to walk through a long list.
#!/bin/bash
while [ $# -gt 0 ]
do
echo "Current: $1"
shift
doneRun ./walk.sh a b c to get a, b, and c on separate lines.
Options With Flags
Many tools accept flags such as -n or -v. The getopts command reads them in a clean way.
#!/bin/bash
while getopts "n:v" option
do
case $option in
n) name=$OPTARG ;;
v) verbose=true ;;
*) echo "Usage: $0 -n name [-v]"; exit 1 ;;
esac
done
echo "Name: $name"
if [ "$verbose" = true ]; then
echo "Verbose mode is on"
fiRun the script like this:
./opts.sh -n Asha -vOutput:
Name: Asha
Verbose mode is onThe letter n: in the pattern needs a value after it. The letter v works as a simple on/off switch.
Practical Example: Add Two Numbers
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 number1 number2"
exit 1
fi
sum=$(( $1 + $2 ))
echo "Sum is $sum"Run ./add.sh 15 27 and the screen shows Sum is 42.
Set Default Values
name=${1:-Guest}
echo "Hello, $name"The script prints Hello, Guest when no argument arrives.
Key Takeaways
- Use
$1,$2, and so on to read arguments by position. $#gives the count and$@gives the full list.- Wrap
"$@"in quotation marks to protect values that contain spaces. shiftremoves the first argument from the list.getoptshandles flags like-nand-v.- Check the argument count before using any value.
