HOWTO · Bash

How to Check if Input Argument Exists in Bash

This tutorial describes checking if a script user has passed an input argument to a Bash script.

On this page

Check a Bash script’s positional parameters to decide whether required arguments were supplied. Use $# to count all arguments, then use $1 when you need the value of the first argument. A count check is the reliable way to reject a call with no arguments; checking $1 answers a different question. The examples below show how to make that distinction before a script starts work that depends on user input.

Count Bash Script Arguments with $#

$# expands to the number of positional arguments supplied to the current function or script. It does not contain an argument value. For example, a script invoked as ./script.sh one two has a count of 2; invoking it without arguments gives a count of 0. This makes $# useful for validating how many values a command received before reading or processing them. It works the same way inside a function: the count then refers to arguments passed to that function.

Run this example with three supplied values:

show_arguments() {
  printf 'Argument count: %s\n' "$#"
  printf 'First argument: %s\n' "$1"
}

show_arguments delft stack blog
Argument count: 3
First argument: delft

The first line reports the number of values; the second prints the first value. For a script that requires at least one value, put a guard before commands that use the arguments. This complete script accepts the values after the guard and reports a clear error when none are provided:

#!/usr/bin/env bash

require_arguments() {
  if (( $# == 0 )); then
    printf '%s\n' 'No input arguments exist' >&2
    return 1
  fi
}

run_check() {
  if ! require_arguments "$@"; then
    return 1
  fi
  printf 'Received %s argument(s).\n' "$#"
  printf 'First argument: %s\n' "$1"
}

run_check delft
Received 1 argument(s).
First argument: delft

This self-contained run supplies delft, so the guard passes and the script reports the count and first value.

In a script that should validate the caller’s actual arguments, use run_check "$@" as the entry point instead. With no arguments, require_arguments prints No input arguments exist to standard error and returns 1; run_check then returns 1 as its final command, so the script also exits with status 1. With one or more arguments, the guard is skipped. You can replace the message or check for a larger required count, for example (( $# < 2 )) when two values are needed. Decide the minimum count from what the rest of your script actually uses, and place validation before side effects such as creating files or changing configuration.

When you call a function, its $# is its own argument count rather than the enclosing script’s count. Pass the values explicitly if the function needs them. This distinction helps when a script has optional flags and then forwards only selected values to a helper. The count is numeric, so Bash’s arithmetic form (( $# == 0 )) reads directly as a comparison and avoids testing the textual contents of a parameter.

Check the First Bash Argument with $1

$1 is the first positional parameter. Quote it when you test or print its value, such as with [[ -n "$1" ]] to check whether its value is nonempty. This check does not tell you whether any argument was supplied: a caller can provide an empty first value, as in ./script.sh '', while $# is still 1. In other words, the position exists even though its value has no characters. Use the count for presence and the value test for content.

Use $# when the requirement is that at least one argument was passed. Use a quoted $1 test when the requirement is that the first argument contains a nonempty value. The positional parameters $1, $2, and $3 refer to the first, second, and third values; quote each one when passing it to another command so spaces within a value remain together. For example, if the first value is North Shore, quoting "$1" keeps it as one value instead of letting the shell split it into separate words.

The two checks can be combined when a task requires both conditions: first confirm (( $# > 0 )), then check [[ -n "$1" ]]. Keeping these decisions separate makes error messages more specific. A script can tell the caller that an argument was omitted, or that a supplied first value must contain text. If the first argument is optional, skip the count guard and decide what a missing value should mean for that particular command. In [[ ... ]], quoting $1 is still recommended for clarity, although word splitting and filename expansion do not occur there. With the single-bracket [ ... ] form, quote expansions such as "$1" because unquoted expansions can undergo word splitting and filename expansion.