-eq
compares integers. Use ==
or =
to compare strings.
if [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
echo "hello"
fi
You could omit the quotes. Variable expansions on the left-hand side of ==
are safe when using double brackets.
You can also use ||
inside the brackets. It's not possible to do that with single brackets, but double brackets are a syntactical feature with special parsing rules that allow it.
if [[ $1 == --help || $1 == -h ]]; then
echo "hello"
fi
If it gets more complicated you might also consider a case
block.
case $1 in
-h|--help)
echo "hello";;
esac
If -eq
is for numerical comparison, how come ./deploy -h
worked as expected?
Arithmetic evaluation normally prints an error message when given illegal expressions, but as it happens the two strings you're asking it to evaluate are syntactically valid.
-h
negates the value of the undefined variable $h
. The result is 0.
--help
is decrements the undefined variable $help
. The result is -1.
Try an invalid string and you'll get an error.
$ ./deploy 'foo bar'
bash: [[: foo bar: syntax error in expression (error token is "bar")
$ ./deploy @
bash: [[: @: syntax error in expression (error token is "@")