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.
Apparently [[ *"$a"* == $b ]] is DIFFERENT from [[ $b == *"$a"* ]]
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"
(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?
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*
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?
Total Comments 4
Comments
-
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 -
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 -
Quote:[...]
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 -
You are welcome, and thanks!
Posted 04-27-2017 at 07:07 PM by astrogeek