this is my first "if-else" script on linux.
It gives the error
[: too many arguments
n=0 if [ $n % 2 -eq 0 ] then echo "even number" fi
How to correct it.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
bash must be performed in
arithmetic context. In your current implementation, you are supplying "$n", "%", and "2" as args to
[, which does not perform math. Here is an example which performs that math n arithmetic context:
if [ "$((n % 2))" -eq 0 ]
In the above example, the results of the
$(()) are expanded by the shell before it is seen by the
[ command. You can simplify this by removing the call to
if (( (n % 2) == 0 ))