bash check command line arguments - Check existence of input argument in a Bash shell script

bash check if variable is empty / bash / shell

I need to check the existence of an input argument. I have the following script

if [ "$1" -gt "-1" ]
  then echo hi
fi

I get

[: : integer expression expected

Brad Parks



Answer #1

Another way to detect if arguments were passed to the script:

((!$#)) && echo No arguments supplied!

In order to exit in the absence of any arguments, one can say:

((!$#)) && echo No arguments supplied! && exit 1

Another (analogous) way to say the above would be:

let $# || echo No arguments supplied

let $# || { echo No arguments supplied; exit 1; }  # Exit if no arguments!
  Evaluate arithmetic expressions.

  ...

  Exit Status:
  If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.