Bash Script Arguments – Use $1, $2, $@, and More in Your Scripts
Introduction to Bash Script Arguments – Access User Inputs Dynamically
Bash script arguments let users pass values into scripts at runtime. These values are captured as positional parameters like $1, $2, $3, etc., and can be accessed, looped over, or validated inside your script.
This allows you to build flexible and interactive scripts that can behave differently based on input values without hardcoding them.
In this article, you’ll learn:
- How to use
$1,$2, and other positional arguments - The meaning of
$@,$*,$#,$0 - How to loop through all arguments
- Real-world examples with argument handling
Positional Parameters in Bash
| Symbol | Meaning |
|---|---|
$0 | Name of the script |
$1 | First argument passed to the script |
$2 | Second argument |
$# | Number of arguments passed |
$@ | All arguments as separate quoted strings |
$* | All arguments as a single word |
Example 1: Basic Argument Access
#!/bin/bash
echo "Script name: $0"
echo "First arg: $1"
echo "Second arg: $2"
echo "Total args: $#"
Run:
./myscript.sh hello world
Output:
Script name: ./myscript.sh
First arg: hello
Second arg: world
Total args: 2
Example 2: Loop Through All Arguments
#!/bin/bash
echo "Listing all arguments:"
for arg in "$@"; do
echo "• $arg"
done
Always quote
"$@"to handle arguments with spaces correctly.
Example 3: Using $* vs $@
#!/bin/bash
echo "\$*: $*"
echo "\$@: $@"
Run with:
./args.sh "a b" c
Output:
$*: a b c
$@: a b c
Difference appears when quoted:
"$*": "a b c"
"$@": "a b" "c"
Best Practices for Script Arguments
| Practice | Why It Helps |
|---|---|
Quote variables like "$1" | Prevents word splitting |
Use $@ for iterating arguments | Treats each argument correctly |
Validate inputs with $# | Avoids missing or extra parameters |
| Show usage if arguments are missing | Helps users run script correctly |
Example: Argument Validation
#!/bin/bash
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <source> <destination>"
exit 1
fi
echo "Copying from $1 to $2"
cp "$1" "$2"
Summary: Bash Script Arguments
Bash script arguments allow your scripts to accept dynamic input, making them powerful tools for automation. Use positional parameters like $1, $2, and $@ to retrieve, loop, and validate input values from the command line.
Key Takeaways:
$1,$2, etc. access specific arguments$#tells how many arguments were passed$@and$*hold all arguments (use"$@"for safety)- Always validate inputs for safe scripting
Real-world Uses:
- Backup tools with dynamic source/target
- Deployment scripts with mode/environment as arguments
- Argument-based logic branching in system utilities
FAQ – Bash Script Arguments
How do I access all arguments passed to a Bash script?
Use "$@" to iterate over all arguments:
for arg in "$@"; do echo "$arg"; done
What does $# represent in Bash?
It shows the number of arguments passed to the script.
Is there a difference between $@ and $*?
Yes.
$@treats each argument separately$*treats them as one string.
Quoted:"$@"="$1" "$2";"$*"="$1 $2"
Can I access arguments beyond $9?
Yes, use braces:
${10}, ${11}, ...
How do I ensure arguments were passed?
Use:
if [[ $# -eq 0 ]]; then echo "No args"; exit 1; fi
Share Now :
