Bash script
Iteration
Scripting
Coding tips
Linux Command-line

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.

Browse interview questions

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.

bash
1#!/usr/bin/env bash
2
3for arg in "$@"; do
4  printf 'argument: %s\n' "$arg"
5done

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.

bash
1#!/usr/bin/env bash
2
3while [[ "$#" -gt 0 ]]; do
4  printf 'current: %s\n' "$1"
5  shift
6done

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 of IFS'

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.

bash
1#!/usr/bin/env bash
2
3usage() {
4  echo "Usage: $0 FILE [FILE ...]" >&2
5  exit 1
6}
7
8[[ "$#" -lt 1 ]] && usage
9
10for file in "$@"; do
11  printf 'processing %s\n' "$file"
12done

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.

bash
1#!/usr/bin/env bash
2
3verbose=false
4
5while getopts ':vo:' opt; do
6  case "$opt" in
7    v) verbose=true ;;
8    o) output="$OPTARG" ;;
9    *)
10      echo "Usage: $0 [-v] [-o output] args..." >&2
11      exit 1
12      ;;
13  esac
14done
15
16shift $((OPTIND - 1))
17
18for arg in "$@"; do
19  printf 'remaining arg: %s\n' "$arg"
20done

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 while plus shift when you need to consume arguments progressively.
  • Learn the difference between "$@" and "$*" because it directly affects correctness.
  • Pair iteration with argument-count checks or getopts when the script has real option parsing needs.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions