On 8.2. 15:05, Jaan Vajakas wrote:
Hi!

I noticed a weird behavior. Is it a bug?

Namely, why does

echo "$(for f in a b c; do if [[ \"$f\" > b ]]; then echo "$f > b"; else
echo "$f <= b"; fi; done)"

If I'm not mistaken, what you're doing is essentially this:

$ for f in '"a"' '"b"' '"c"'; do if [[ $f > b ]]; then echo "$f > b"; else echo "$f <= b"; fi; done

i.e., since you added the escaped quotes, you're testing strings with quotes against a string without one. The result is very likely to depend on your locale.

With LC_COLLATE="en_US.UTF-8", I get:

"a" <= b
"b" > b
"c" > b

Here, the first quote is effectively ignored unless it's the only difference.


With LC_COLLATE=C, I get

"a" <= b
"b" <= b
"c" <= b

Here, the comparison stops when the first quote is compared against the letter b.


I'm not sure what the command substitution is supposed to do here, apart from confusing the command. The quotes inside the command substitution are independent of the quotes outside of it. I'm not sure if you already know this or not, since you only escaped _some_ of them.

I would have expected the same output as one of
echo "$(for f in a b c; do if [[ "$f" > b ]]; then echo "$f > b"; else echo
"$f <= b"; fi; done)"

Here, there are no quotes in the string to be compared, you've just quoted the string on the left-hand side. The quotes are removed as usual before the string is compared.

echo "$(for f in a b c; do if [[ '"$f"' > b ]]; then echo "$f > b"; else
echo "$f <= b"; fi; done)"

Here, the left-hand side is single-quoted, so the variable is not expanded, and the comparison is of a fixed string against another.

This happens e.g. on GNU Bash v4.4 (e.g.
https://www.tutorialspoint.com/execute_bash_online.php ).


Best regards,
Jaan



--
Ilkka Virta / itvi...@iki.fi

Reply via email to