Search code examples
linuxbashshellequality

Unexpected output for bash argument equality check


I have a basic string equality check in a Bash script, but the output is not as expected.

To reproduce, copy the code below into an executable file (called 'deploy' in my examples below).

#!/bin/bash

echo $1

if [[ "$1" -eq "--help" ]] || [[ "$1" -eq "-h" ]]; then
    echo "hello"
fi


If I run the script like so:

./deploy -h

The output is:

-h
hello

If I run the script like so:

./deploy --help

The output is:

-help

Why does the conditional statement not resolve to true?


Solution

  • -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 "@")