How to iterate over arguments in a Bash script
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Iterating over arguments is one of the first real Bash scripting skills because many scripts need to process a variable number of inputs. The safest default is to iterate over "$@", which preserves each original argument exactly, including arguments that contain spaces.
Use "$@" for a Straightforward Loop
If you simply want to process each argument in order, a for loop over "$@" is the cleanest solution.
This works because "$@" expands to each positional argument as a separate quoted word. That is exactly what you want when arguments may contain spaces, tabs, or wildcard characters.
By contrast, unquoted $@ or $* can trigger word splitting and change the meaning of the input.
Use while and shift When You Need Stateful Parsing
If the script needs to consume arguments one by one, while plus shift is often better.
shift discards the current $1, moves $2 into $1, and decrements $#. This is especially useful when parsing flags, subcommands, or paired values such as --output file.txt.
Know the Special Argument Variables
A few variables matter constantly in Bash argument handling:
- '
$#is the number of positional arguments' - '
$1, $2, and so on are the positional arguments themselves' - '
"$@"expands to all arguments, preserving their boundaries' - '
"$*"combines them into a single string using the first character ofIFS'
That last point is why "$*" is usually not the right tool for iteration. It is useful when you intentionally want one combined string, not when you want to process arguments individually.
Combine Iteration with Validation
Most real scripts should check the argument count before iterating.
This avoids the common problem of quietly doing nothing when the caller forgot to provide the required inputs.
Prefer getopts for Real Options
If the script supports flags such as -v or -o, plain iteration may not be enough. getopts gives you structured option parsing.
The pattern here is: parse options first, shift them away, then iterate over the remaining positional arguments.
If your script supports the -- separator, handle that explicitly during parsing. It is the standard way to tell many command-line tools that option processing is finished and that the remaining values should be treated as plain positional arguments.
Common Pitfalls
Using unquoted $@ is the most common mistake because arguments containing spaces get split into multiple words.
Using $* when you actually need separate arguments changes the semantics of iteration.
Mixing shift-based parsing with later positional access without thinking about the new argument positions often leads to subtle bugs.
Summary
- Use
for arg in "$@"for the simplest and safest argument iteration. - Use
whileplusshiftwhen you need to consume arguments progressively. - Learn the difference between
"$@"and"$*"because it directly affects correctness. - Pair iteration with argument-count checks or
getoptswhen the script has real option parsing needs.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.