LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Apparently [[ *"$a"* == $b ]] is DIFFERENT from [[ $b == *"$a"* ]]

Posted 04-25-2017 at 11:11 PM by the dsc
Updated 04-25-2017 at 11:25 PM by the dsc

a="foo/bar" ; b="foo/bar/gee" ; [[ *"$a"* == $b ]] && echo -e "\n 1 - b contains a; *a* = b \n" ; [[ $b == *"$a"* ]] && echo -e "\n 2 - b contains a ; b = *a* \n"

Code:
$ a="foo/bar" ; b="foo/bar/gee" ; [[ *"$a"* == $b ]] && echo -e "\n 1 - b contains a; *a* = b \n" ; [[ $b == *"$a"* ]] && echo -e  "\n 2 - b contains a ; b = *a* \n"[/B]

 2 - b contains a ; b = *a*
(The first "$" is representing the user's command prompt)

GNU bash, version 4.4.11(1)-release (x86_64-pc-linux-gnu)

Is it some sort of bug, or is there some sort of genius-level (or... above fifth grade) math/computer science stuff that implies that these are really two different logical statements, somehow?
Posted in Uncategorized
Views 718 Comments 4
« Prev     Main     Next »
Total Comments 4

Comments

  1. Old Comment
    Yikes. I feel your pain. I use bash every day but I just don't trust it to be consistent in its syntax. I think I understand something in bash and then ... it does something squirrely and unexpected.
    Posted 04-26-2017 at 05:32 PM by jr_bob_dobbs jr_bob_dobbs is offline
  2. Old Comment
    Actually, that is not odd at all, and yes they are two totally different tests.

    The result is due to ordinary associativity and precedence rules. (About fifth grade concepts ). The equality test is left associative - it evaluates the left side first, then compares the right side to what was on the left (it has to start somewhere!).

    The first test is actually "a equals b", not "b contains a" as your printed string says.

    This is because the equal test is left associative - the thing on the left is evaluated first, then the thing on the right is tested for equality to it.

    The thing on the left is *"$a"*, or $a within something else... but the something else here is null, so it is just "$a", or "foo/bar". Then the thing on the right $b is evaluated for equality with exactly that, "foo/bar", and "foo/bar/gee" is indeed not equal to that!

    The second test is different, "b equals (a with optional leading and trailing characters)".

    By associativity, $b is evaluated first, it is "foo/bar/gee". Then the thing on the right *"foo/bar"* is evaluated and tested for equality with the thing on the left. "foo/bar" inside something else (i.e. with optional leading or trailing characters) is indeed equal to "foo/bar/gee".

    Hope this helps!
    Posted 04-27-2017 at 05:54 PM by astrogeek astrogeek is offline
  3. Old Comment
    Quote:
    Originally Posted by astrogeek View Comment
    [...]

    By associativity, $b is evaluated first, it is "foo/bar/gee". Then the thing on the right *"foo/bar"* is evaluated and tested for equality with the thing on the left. "foo/bar" inside something else (i.e. with optional leading or trailing characters) is indeed equal to "foo/bar/gee".

    Hope this helps!

    It definitely does! It's a shame I can't upvote blog comments, apparently.
    Posted 04-27-2017 at 05:57 PM by the dsc the dsc is offline
  4. Old Comment
    You are welcome, and thanks!
    Posted 04-27-2017 at 07:07 PM by astrogeek astrogeek is offline
 

  



All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration