2

I am writing a shell script and have come across some weird if behavior.

My question is simple, why does [[ "╝" = [█] ]] evaluate to true? Is it because they are both uncommon ascii characters? On my mac, it evaluates fine.

note: The █ does need to be in brackets.

2
  • 1
    Looks like a bug.
    – choroba
    Feb 14 '15 at 19:00
  • I recommend submitting a bug report.
    – 4ae1e1
    Feb 17 '15 at 3:59
2

The evaluation does not depend on platform (mac or other). It depends on current locale settings:

$ LANG=C
$ if [[ "╝" = [█] ]]; then echo y; else echo n; fi
n
$ LANG=en_US.UTF-8
$ if [[ "╝" = [█] ]]; then echo y; else echo n; fi
y

We can try to detect which locales matches the expression via simple script:

for i in $(locale -a)
do
   export LANG=$i
   echo -n "LANG=$LANG   "
   if [[ "╝" = [█] ]]; then echo yes; else echo no; fi
done

and see that the UTF-8 locales matches the expression. When locale is set to UTF-8 capable then bash interprets the UTF-8 characters in different way and that is the reason why the result are different.

Nevertheless I think it is a bug because it should return false even if locale with UTF-8 support is set.

3
  • It is a bug. And it does depend on the platform and version. I can reproduce this on my Ubuntu 14.04LTS with bash 4.3.30, but not on OS X with either system bash (3.2.57(1), back from 2007) or brewed bash (4.3.33(1)): bash -c 'LANG=en_US.UTF-8; [[ "╝" = [█] ]]' or bash -c 'LANG=C; [[ "╝" = [█] ]]' both return 1 no matter what.
    – 4ae1e1
    Feb 17 '15 at 3:56
  • Also, I tried to blame this bug on encoding but failed. Even if you disassemble both UTF-8 characters into bytes, the first string won't match the second string (considered as a [] pattern). Maybe we have to look at the source code to tell what's exactly wrong.
    – 4ae1e1
    Feb 17 '15 at 3:58
  • I submitted a bug report, but I have very limited experience in non scripting languages so I'm afraid I won't be much help in looking for the problem in the source.
    – Lattis
    Feb 17 '15 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.