Command Line Arguments in Shell Scripting
Pass values to a Bash script at the time of running it, and read them inside the script with $0, $1, $2, $# and $*.
So far, every value our scripts used was either written inside the script or typed in by the user through read. Both have a limit. A hardcoded value means editing the script every time. A read prompt means somebody has to sit in front of the terminal and answer it, which does not work when the script runs from another script, a cron job or a CI pipeline.
Command line arguments solve this. You write the values right after the script name when you run it, and the script picks them up on its own:
./telusko.sh linux devops scripting awsHere linux, devops, scripting and aws are four arguments. "Argument" is only another word for the data you pass. They are called command line arguments because they come from the command line, the place where you execute the script.
How the script reads them
Inside the script, Bash gives you a set of ready-made variables. You do not declare them. They are filled in automatically every time the script starts.
| Symbol | Meaning |
|---|---|
$0 | The script name, exactly as it was typed |
$1 | The first argument |
$2 | The second argument |
$3, $4 ... | The third, fourth argument and so on |
$# | The total number of arguments |
$* | All the arguments together |
$1, $2 and the rest are called positional parameters, because the value you get depends only on the position of the argument on the command line.
Step 1: Print the arguments
Create a script inside your scripts folder:
vi telusko.sh#!/bin/bash
echo "The Script file name : $0 "
echo "The first arg : $1"
echo "The second arg : $2"
echo "All Command line args : $*"
echo "Total number of args: $#"Give it the execute permission and run it with four arguments:
chmod u+x telusko.sh
./telusko.sh linux devops scripting awsThe Script file name : ./telusko.sh
The first arg : linux
The second arg : devops
All Command line args : linux devops scripting aws
Total number of args: 4Read the output against the command you typed:
$0is./telusko.sh, the script itself. It includes the./because that is how the script was called.$1and$2arelinuxanddevops. The script never printed$3and$4, but they are still there, holdingscriptingandaws.$*shows all four arguments in one line.$#is4. The script name is not counted.
Run the same script again with different words and the output changes. The script file stayed the same. That is what "dynamic" means here.
Step 2: A more realistic script
Printing arguments is a demo. Let's use them for real work: a script that adds two numbers given on the command line.
vi add.sh#!/bin/bash
RESULT=$(($1+$2))
echo "SUM of 2 numbers is : $RESULT"$(( ... )) is arithmetic expansion, which you saw in Operators in Shell Scripting. Bash calculates what is inside and the answer is stored in RESULT.
chmod u+x add.sh
./add.sh 44 54SUM of 2 numbers is : 98Try ./add.sh 10 20 and you get 30, without opening the file.
What happens when an argument is missing
The lecture always passes the right number of arguments. In real use, somebody will forget one. Run add.sh with a single number:
./add.sh 44./add.sh: line 3: 44+: syntax error: operand expected (error token is "+")
SUM of 2 numbers is :$2 is empty, so Bash tried to calculate 44+ and failed. Two things are worth noticing. A missing argument is not an error by itself, it quietly becomes an empty string. And even after the failed calculation the script did not stop. It went on to the next line and printed a sum with nothing in it.
The fix is to check $# at the top and stop with a clear message:
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <number1> <number2>"
exit 1
fi
RESULT=$(($1+$2))
echo "SUM of 2 numbers is : $RESULT"./add.sh 44Usage: ./add.sh <number1> <number2>Notice the use of $0 in the message. If someone renames the script, the usage line still shows the correct name. exit 1 ends the script with a non-zero status, which tells whoever called it that something went wrong.
Arguments with spaces
The shell splits the command line on spaces. If one argument contains a space, wrap it in quotes:
./telusko.sh "shell scripting" awsThe first arg : shell scripting
The second arg : aws
Total number of args: 2Without the quotes, shell and scripting would be two separate arguments and the count would be 3.
$* and $@
The lecture uses $* for all arguments. There is a second symbol, $@, that you will see even more often. Without quotes, both behave the same. Inside double quotes they are different:
| Form | Result for ./script.sh "shell scripting" aws |
|---|---|
"$*" | One single string: shell scripting aws |
"$@" | Each argument kept separate: shell scripting and aws |
This matters the moment you loop over the arguments:
#!/bin/bash
for arg in "$@"
do
echo "Argument: $arg"
done./args-loop.sh "shell scripting" awsArgument: shell scripting
Argument: awsWith "$*" the loop would run only once, with everything joined together. For printing a message, $* is fine. For looping or passing the arguments on to another command, use "$@".
Beyond the ninth argument
$10 does not mean the tenth argument. Bash reads it as $1 followed by the character 0. From the tenth argument onwards, use braces: ${10}, ${11} and so on.
Quick reference
| Symbol | Meaning | Value for ./telusko.sh linux devops scripting aws |
|---|---|---|
$0 | Script name | ./telusko.sh |
$1 | First argument | linux |
$2 | Second argument | devops |
$# | Number of arguments | 4 |
$* | All arguments | linux devops scripting aws |
"$@" | All arguments, each one kept separate | linux devops scripting aws |
${10} | Tenth argument | empty, only four were passed |
Try It Yourself
- Create
telusko.shas above and run it with four arguments. Then run it with no arguments at all and see what$1and$#print. - Add
$3and$4to the script and confirm they printscriptingandaws. - Create
add.sh, run./add.sh 44 54, then run it with only one number and read the error. - Add the
$#check toadd.shand run it again with one number. Check the exit status right after withecho $?. - Write
greet.shthat takes a name and a course, and printsWelcome <name> to the <course> course. Call it with a course name that has a space in it.
Official references
Written By: Shramik Masti
How is this guide?
Last updated on
