---
title: "How do I pass arguments to a Bash script?"
description: "Read script arguments with "
url: https://www.danny.engineering/answer/bash/how-do-i-pass-arguments-to-a-bash-script
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:23:00.000Z
tags: ["Bash"]
---

# How do I pass arguments to a Bash script?

## The short answer


Words after the script name arrive as positional parameters. The first is `$1`, the second `$2`, and so on. `$#` is how many there are, and `"$@"` is all of them, each kept as a separate word.


```bash
#!/usr/bin/env bash
echo "Script: $0"
echo "First:  $1"
echo "Count:  $#"
for arg in "$@"; do
  echo "Arg: $arg"
done
```


```bash
./greet.sh alice "bob smith"
```


## Why it happens


When you run a script, the shell splits the command line into words, using quotes to decide where each word starts and ends, and passes them to the script. Inside the script they are available as numbered variables:


| Variable     | Meaning                                  |
| ------------ | ---------------------------------------- |
| `$0`         | the name the script was run as           |
| `$1` to `$9` | the first nine arguments                 |
| `${10}`      | the tenth and later; braces are required |
| `$#`         | the number of arguments                  |
| `"$@"`       | every argument as a separate word        |
| `"$*"`       | every argument joined into one string    |


The quotes matter. `"$@"` keeps `"bob smith"` as one argument. Unquoted `$@` or `$*` splits it into `bob` and `smith`, and expands any glob characters. When passing arguments on to another command, `"$@"` is almost always what you want.


## How to fix it


### Required arguments and defaults


Check the count and print a usage message when something is missing:


```bash
if (( $# < 1 )); then
  echo "Usage: $0 source [destination]" >&2
  exit 1
fi

src=$1
dest=${2:-./backup}   # default when the second argument is missing or empty
```


A shorter way to require an argument is the `:?` expansion, which stops the script with your message if the value is missing:


```bash
src=${1:?Usage: backup.sh source [destination]}
```


Give positional parameters names early. `$src` is much easier to follow than `$1` twenty lines later.


### Loop with shift


`shift` drops `$1` and moves every other argument down one place. That makes it easy to handle a leading option and then treat the rest as a list:


```bash
verbose=false
if [[ "$1" == "-v" ]]; then
  verbose=true
  shift
fi

for file in "$@"; do
  $verbose && echo "Processing $file"
  wc -l "$file"
done
```


### Parse short flags with getopts


For real options such as `-v` or `-o output.txt`, use the built-in `getopts`. The option string lists each letter; a colon after a letter means it takes a value. A leading colon lets you handle errors yourself.


```bash
#!/usr/bin/env bash
usage() {
  echo "Usage: $0 [-v] [-o output] file..." >&2
  exit 1
}

verbose=false
output="out.txt"

while getopts ":vo:h" opt; do
  case $opt in
    v) verbose=true ;;
    o) output=$OPTARG ;;
    h) usage ;;
    :) echo "Option -$OPTARG needs a value" >&2; usage ;;
    \?) echo "Unknown option -$OPTARG" >&2; usage ;;
  esac
done
shift $((OPTIND - 1))   # remove the parsed options

(( $# > 0 )) || usage
echo "Writing $# files to $output"
```


After the `shift`, `"$@"` contains only the remaining arguments. `getopts` also handles combined flags like `-vo out.txt` for you.


### Long options


`getopts` only supports single-letter options. For flags like `--output`, a `while` loop with `case` and `shift` is the usual approach:


```bash
while (( $# > 0 )); do
  case $1 in
    -o|--output) output=$2; shift 2 ;;
    -v|--verbose) verbose=true; shift ;;
    --) shift; break ;;
    -*) echo "Unknown option $1" >&2; exit 1 ;;
    *) break ;;
  esac
done
```


## Common pitfalls

- Writing `$10` and getting the first argument followed by a zero. Use `${10}`.
- Using unquoted `$@` or `$*`, which breaks arguments containing spaces.
- Forgetting `shift $((OPTIND - 1))` after `getopts`, so the options are still in `"$@"`.
- Reading `$1` when it might be missing under `set -u`. Use `${1:-}` to allow an empty value.
- Expecting `getopts` to understand `--long` options. It does not.
- Passing arguments with `source script.sh`. The arguments replace the current shell's positional parameters only for the duration of the sourced file, which is rarely what you intended.

## Related reading

- [GNU Bash manual: Positional parameters](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html)
- [GNU Bash manual: Bourne shell builtins, getopts](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html)
