If your
$VARIABLE
is a string containing spaces or other special characters,
and single square brackets are used (which is a shortcut for the
test
command), then the string may be split out into multiple words. Each of these is treated as a separate argument.
So that one variable is split out into many arguments.
The same will be true for any function call that puts down a string containing spaces or other special characters.
Easy fix
Wrap the variable output in double quotes, forcing it to stay as one string (therefore one argument). For example,
VARIABLE=$(/some/command);
if [ "$VARIABLE" == 0 ]; then
# some action
fi
An alternate fix is to use double square brackets (which is a shortcut for the new test
command).
This exists only in bash (and apparently korn and zsh) however, and so may not be compatible with default shells called by /bin/sh
etc. This means on some systems, for example, it might work from the console but not from cron
, depending on how everything is configured.
It would look like this:
VARIABLE=$(/some/command);
if [[ $VARIABLE == 0 ]]; then
# some action
fi
http://stackoverflow.com/questions/13781216/bash-meaning-of-too-many-arguments-error-from-if-square-brackets