Q: Bash script: If any argument ($@) equals "foo" then… throws "too many arguments" In bash I can grab first argument via $1, second via $2 etc.
I want to avoid hardcoding the amount of arguments because they can come in random order. Hence I wrote:
if [ "$@" == "--no-update" ]; then # this is line 14
    warning "The '--no-update' flag detected. Pull from origin master skipped."
else
    git pull origin master
fi

I call the script via:

sh ~/code/dir/script.sh -f --no-update

but the args can also come in different order or quantity, like:

sh ~/code/dir/script.sh -f -what-ever -abc --no-update -a -f -maybe-this

The output throws:

line 14: [: too many arguments

What am I doing wrong?

A: "$@" is a bit special in that it expands to multiple 'fields'.
If you run your script with the arguments:
-f --no-update

Then:
[ "$@" = "--no-update" ]

Expands to:
[ "-f" "--no-update" = "--no-update" ]
# ⬑1   ⬑2

And [ doesn't know what to do with that (it can't compare two strings to one string), hence 'too many arguments'.

To test against all the arguments you could use:
for arg
do
  [ "${arg}" = "--no-update" ] &&
    warning "The '--no-update' flag detected. Pull from origin master skipped."
done

