๐งพ 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 :
