87

I just want to check if one parameter was supplied in my bash script or not.

I found this, but all the solutions seem to be unnecessarily complicated.

What's a simple solution to this simple problem that would make sense to a beginner?

Community
  • 1
  • 1
node ninja
  • 31,796
  • 59
  • 166
  • 254
  • ^answers from above possible duplicate are similar to the ones posted here including a comment about using the [[ -z ]] test – pd12 Jan 11 '16 at 21:53

3 Answers3

113

Use $# which is equal to the number of arguments supplied, e.g.:

if [ "$#" -ne 1 ]
then
  echo "Usage: ..."
  exit 1
fi

Word of caution: Note that inside a function this will equal the number of arguments supplied to the function rather than the script.

EDIT: As pointed out by SiegeX in bash you can also use arithmetic expressions in (( ... )). This can be used like this:

if (( $# != 1 ))
then
  echo "Usage: ..."
  exit 1
fi
Community
  • 1
  • 1
Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
  • 3
    since this is `bash` question I would suggest `if (( $# != 1 ))` – SiegeX Jan 23 '12 at 08:30
  • For a range, a `case` statement may be more idiomatic. The syntax looks alien to beginners, though. `case $# in [123]) ;; *) echo fail >&2; exit 1;; esac` – tripleee Jan 23 '12 at 11:07
  • 15
    Expressions in `[]` use `<`, `>`, `!=` and `==` for string comparisons and `-lt`, `-gt`, `-le`, `-ge`, `-ne` and `-eq` for arithmetic comparisons. Expressions in `(())` use `<`, `>`, `!=` and `==` for arithmetic comparisons. For more operators see [bash manpage](http://linux.die.net/man/1/bash). – Adam Zalcman Jan 23 '12 at 17:53
39

The accepted solution checks whether parameters were set by testing against the count of parameters given. If this is not the desired check, that is, if you want to check instead whether a specific parameter was set, the following would do it:

for i in "$@" ; do
    if [[ $i == "check parameter" ]] ; then
        echo "Is set!"
        break
    fi
done

Or, more compactly:

for i in "$@" ; do [[ $i == "check argument" ]] && echo "Is set!" && break ; done
rybo111
  • 12,240
  • 4
  • 61
  • 70
h7r
  • 4,944
  • 2
  • 28
  • 31
  • 3
    I'm not sure your method is correct… for example: `./script check parameter` will succeed (which is incorrect). – gniourf_gniourf Feb 01 '15 at 13:29
  • @gniourf_gniourf You are fully right. I edited with a solution that actually does the trick. Unfortunatelly with a loop. – h7r Feb 01 '15 at 17:53
  • you want it compact, there is this weird: `for i do [ "$i" == ...` probably just to confuse people [tldp](https://www.tldp.org/LDP/abs/html/loops1.html#EX23) – papo Dec 02 '18 at 22:02
14
if (( "$#" != 1 )) 
then
    echo "Usage Info:…"
exit 1
fi
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • Thanks @SiegeX .. have updated the answer. Out of curiosity, any particular difference I should be aware of? – jaypal singh Jan 23 '12 at 08:34
  • Only that it supports superior syntax such as `==` `!=` `<` `>` `<=` `>=` and it can also do boolean logic inside a single `(( ))` much like you can do in C – SiegeX Jan 23 '12 at 08:48