dimanche 16 décembre 2018

Bash does into unexpected recursion when processing expression inside [[

I am trying to understand the logic of Bash algorithm.

When I tried this, it printed "a":

a=a;[ $a == "a" ] && echo $a

So far so good. Then I tried the following and it printed "a" again:

a=a;[[ $a == "a" ]] && echo $a

Now I introduced an error by using arithmetic comparison:

a=abc;[ $a -eq "abc" ] && echo $a

I got an error message that makes sense:

-bash: [: abc: integer expression expected

Then I tried to do this with double bracket and got no error, but "abc":

a=abc;[[ $a -eq "abc" ]] && echo $a

I can sort of explain it (bash is trying to be accomodating), but then I got something that puzzles me. If I do that, I get an error message about recursion:

a=a;[[ $a -eq "a" ]] && echo $a

-bash: [[: a: expression recursion level exceeded (error token is "a")

If I use single brackets, there is no recursion but a reasonable error "integer expression expected":

a=a;[ $a -eq "a" ] && echo $a
-bash: [: abc: integer expression expected

This is weird. What Bash is trying to do in that "recursion" case with double brackets? I am talking about:

a=a;[[ $a -eq "a" ]] && echo $a
-bash: [[: a: expression recursion level exceeded (error token is "a")

Aucun commentaire:

Enregistrer un commentaire