What Are Positional Parameters in Shell Scripting?
Positional Parameters : Bash or shell scripting, positional parameters allow you to access arguments passed to a script or function. They are incredibly useful when you want to handle user inputs dynamically.
Why Learn Positional Parameters?
- 📦 Helps you write flexible scripts
- 🧑💻 Makes your scripts accept inputs
- 🔁 Supports automation and reusability
- 🧩 Essential for system scripting and DevOps
Key Positional Parameters Explained
Parameter | Meaning |
---|---|
$1 , $2 , … | First, second, etc., arguments |
$0 | Name of the script |
$# | Total number of arguments |
$@ | All arguments as separate words |
$* | All arguments as a single word |
"$@" | Preserves spaces and treats arguments individually |
"$*" | Merges all arguments into one string |
Basic Script Example
#!/bin/bash
echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Total Arguments: $#"
echo "All Arguments (separately): $@"
echo "All Arguments (together): $*"
Run the script like this:
bash myscript.sh apple banana cherry
Output:
Script Name: myscript.sh
First Argument: apple
Second Argument: banana
Total Arguments: 3
All Arguments (separately): apple banana cherry
All Arguments (together): apple banana cherry
Using "${@}"
vs "${*}"
Let’s see the difference:
Example Script:
#!/bin/bash
echo "Using \"\$@\":"
for arg in "$@"; do
echo "$arg"
done
echo "Using \"\$*\":"
for arg in "$*"; do
echo "$arg"
done
Run with:
bash myscript.sh "hello world" "foo bar"
Output:
Using "$@":
hello world
foo bar
Using "$*":
hello
world
foo
bar
📌
"$@"
keeps the full argument structure."$*"
splits everything.
Bonus: Accessing All Arguments with a Loop
#!/bin/bash
echo "You passed $# arguments."
i=1
for arg in "$@"; do
echo "Argument $i: $arg"
((i++))
done
Best Practices
- Always quote your parameters:
"$1"
,"$@"
- Use
$#
to validate inputs - Use loops with
"$@"
for safe iteration
Real-world Use Case: Validate Arguments
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <filename> <username>"
exit 1
fi
echo "Creating file $1 for user $2..."
5 Practical Examples Using Positional Parameters in Shell Scripts
Example 1: Greeting Script with $1
and $2
#!/bin/bash
echo "Hello, $1!"
echo "Welcome to $2."
Run:
bash greet.sh Nish India
Output:
Hello, Nish!
Welcome to India.
Example 2: Check If Arguments Are Passed Using $#
#!/bin/bash
if [ "$#" -lt 2 ]; then
echo "❌ Error: At least 2 arguments required."
echo "Usage: $0 <arg1> <arg2>"
exit 1
fi
echo "You passed $#: $1 and $2"
Run:
bash check.sh first
Output:
❌ Error: At least 2 arguments required.
Usage: check.sh <arg1> <arg2>
Example 3: Loop Through All Arguments Using $@
#!/bin/bash
echo "📦 List of arguments:"
for item in "$@"; do
echo "- $item"
done
Run:
bash list.sh Mango Banana Apple
Output:
📦 List of arguments:
- Mango
- Banana
- Apple
Example 4: Sum Two Numbers Using Positional Parameters
#!/bin/bash
num1=$1
num2=$2
sum=$((num1 + num2))
echo "The sum of $num1 and $num2 is: $sum"
Run:
bash sum.sh 5 15
Output:
The sum of 5 and 15 is: 20
Example 5: Handle All Arguments as One String Using $*
#!/bin/bash
echo "🧵 All arguments as one string: $*"
Run:
bash all.sh "one two" "three four"
Output:
All arguments as one string: one two three four
Summary Table
Symbol | What It Does | Example |
---|---|---|
$1 | First argument | echo $1 |
$2 | Second argument | echo $2 |
$# | Argument count | echo $# |
$@ | All arguments (safe loop) | for i in "$@" |
$* | All arguments (single string) | echo $* |
Conclusion
Mastering positional parameters in shell scripting is essential for writing interactive, powerful, and reusable scripts. Always remember:
$1
,$2
, etc. — access specific inputs$#
— count of arguments"$@"
— iterate safely- Use
"$*"
when combining arguments into a single string
Frequently Asked Questions (FAQ)
Q1: What are positional parameters in shell scripting?
Positional parameters are special variables in shell scripts that store command-line arguments passed to the script. For example, $1
is the first argument, $2
is the second, and so on.
Q2: What does $0
mean in a shell script?
$0
represents the name of the script itself. It’s often used in usage messages or logs to display which script is running.
Q3: What is the difference between $@
and $*
?
$@
treats each argument as a separate string, ideal for loops.$*
treats all arguments as one single string, often used in quotes.
Example:
for arg in "$@"; do echo "$arg"; done # Handles each argument correctly
for arg in "$*"; do echo "$arg"; done # May split arguments incorrectly
Q4: How do I check how many arguments were passed to the script?
Use $#
to get the number of arguments.
echo "You passed $# arguments."
Q5: Can I access more than 9 arguments like $10
, $11
, etc.?
Yes, but use braces to avoid ambiguity.
echo "Tenth argument: ${10}"
Q6: How can I loop through all arguments in a script?
for arg in "$@"; do
echo "Argument: $arg"
done
This safely loops through all arguments with proper quoting.
Q7: What happens if an argument is missing in $1
, $2
, etc.?
It returns an empty string. Always validate input count using $#
before accessing specific arguments.
Q8: Are positional parameters only available in Bash?
No, they’re available in all POSIX-compliant shells, including sh, Bash, Zsh, and Ksh.
Q9: How can I print all arguments including the script name?
echo "Script: $0"
echo "Arguments: $@"
Q10: Can I shift positional parameters inside a script?
Yes! Use shift
to move parameters leftward:
shift
echo "Now \$1 is: $1"
You can also Visit other tutorials of Embedded Prep
- What is eMMC (Embedded MultiMediaCard) memory ?
- Top 30+ I2C Interview Questions
- Bit Manipulation Interview Questions
- Structure and Union in c
- Little Endian vs. Big Endian: A Complete Guide
- Merge sort algorithm
Special thanks to @embedded-prep for contributing to this article on Embedded Prep
Leave a Reply