Tuesday, 21 February 2012

Bash Test error "[: too many arguments"

I was writing a shell script which checks whether a variable is null or not using:

 if [ -n $diffCurr ]

When I was running the script I was getting an error as follows:

[: too many arguments


This was happening because I have left out quotation marks around the variable that I passed into test (otherwise known as [)

This means if the variable is empty, it will be as if I had no arguments ( [ -n ] ) and if the variable contains spaces, it will be as if I passwd multiple arguments.

so to resolve the issue I used:

if  [ -n "$diffCurr" ]

Hope this helps me or someone in future if the silly mistake is committed.

No comments:

Post a Comment