if a script has
if [ $1 == "-?" ]; then #line 4
echo "usage: ...."
fi
when the script get runs without any parameter, it will complain that
./script.sh: line 4: [: ==: unary operator expected
but if instead
if [ "$1" == "-?" ]; then #line 4
echo "usage: ...."
fi
then everything’s fine
why is that?
thanks
If the first argument is missing or empty, your first script evaluates to:
… which is a syntax error. As you noticed, to prevent that you need to make use of
"", then it evaluates to:AFAIK this is due to the way the original Bourne shell was working. You should make it a habit of enclosing variables in
""to also work correctly with arguments that have spaces in it. For example, if you would call your script like this:Then your first script would evaluate to:
which is also a syntax error. Also things like
rm $1will not do what you want if you pass filenames with spaces. Dorm "$1"instead.