lundi 5 mars 2018

Bash variable test operator(s) not working as expected

I'm running Bash 4.4.19, under Fedora 27, and I have a simple script:

#!/bin/bash
TESTENV="Hello"
if [ -v $TESTENV ]; then
    echo "Yup"
fi
echo "Done"

When I run this, it prints:

./myscript: line 3: [: Some: binary operator expected
Done

So, I add the extra bracket to line 3, which now looks like:

if [[ -v $TESTENV ]]; then

But produces:

Done

What gives? I expected to see "Yup" for one/both of the above. I've tried wrapping "$TESTENV" in quotes, and I've also tried the -z operator instead - but the behavior is the same.

Aucun commentaire:

Enregistrer un commentaire