bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Quoting and string comparison


From: Ilkka Virta
Subject: Re: Quoting and string comparison
Date: Thu, 8 Feb 2018 20:56:04 +0200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

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 / address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]