0

I made a simple script, just to test if the user passed more than 2 arguments.

My script is the following

#!/bin/sh

if (( $# > 2 )); then
  echo "greater"
else
  echo "not greater"
fi

It should be really simple, since $# returns the number of arguments, but I receive the following error:

schwarz@peano:~$ ./test.sh
./test.sh: 3: 0: not found
not greater

schwarz@peano:~$ ./test.sh one
./test.sh: 3: 1: not found
not greater

schwarz@peano:~$ ./test.sh one two
./test.sh: 3: 2: not found
not greater

schwarz@peano:~$ ./test.sh one two three
./test.sh: 3: 3: not found
not greater

schwarz@peano:~$ ./test.sh one two three four
./test.sh: 3: 4: not found
not greater

It says "not found" to the number of arguments I pass. What I'm doing wrong?

Thanks in advance.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • 1
    `sh` is not bash. This is a bash feature you're trying to use. You need to run your script with bash, not sh; change `#!/bin/sh` to `#!/usr/bin/env bash` – Charles Duffy Feb 03 '23 at 01:38
  • Alternatively, the sh equivalent is `[ "$#" -gt 2 ]` instead of `(( $# > 2 ))`. – Charles Duffy Feb 03 '23 at 01:41
  • 1
    Use [Shellcheck](https://www.shellcheck.net/) to find this error ([SC3006](https://www.shellcheck.net/wiki/SC3006)), and many other common shell programming errors. – pjh Feb 03 '23 at 01:49

0 Answers0