๐Ÿ”ง 4. Bash Functions & Scripting
Estimated reading: 3 minutes 40 views

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

SymbolMeaning
$0Name of the script
$1First argument passed to the script
$2Second 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

PracticeWhy It Helps
Quote variables like "$1"Prevents word splitting
Use $@ for iterating argumentsTreats each argument correctly
Validate inputs with $#Avoids missing or extra parameters
Show usage if arguments are missingHelps 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐ŸŸข Bash: Script Arguments ($1, $2, etc.)

Or Copy Link

CONTENTS
Scroll to Top