2

I want to check if the user has given the script any arguments and if this is the case, the script should close.

if [ $@ = "" ]; then
    exit
fi

is not working.

chepner
  • 497,756
  • 71
  • 530
  • 681
Craxxurz
  • 105
  • 8
  • 1
    The number of arguments are stored in the variable `$#` – Fredrik Pihl Jan 21 '16 at 13:39
  • Note, the reason this doesn't work, is that you need to put double quotes around `$@` because of how bash handles empty strings. Unquoted, the first line becomes `if [ = "" ]; then` when no arguments are passed in, which results in a syntax error. Quoted, you have `if [ "" = "" ]; then`, which is treated as expected. – Zoey Hewll Oct 04 '17 at 06:02

1 Answers1

2

You can try like this,

if [ "$#" -eq 0 ]; then
    echo "Illegal number of parameters"
fi
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42