* bash string-operator problem
@ 2019-02-21 16:10 Rockefeller, Harry
2019-02-21 16:18 ` Eric Blake
2019-02-21 16:56 ` Lee
0 siblings, 2 replies; 7+ messages in thread
From: Rockefeller, Harry @ 2019-02-21 16:10 UTC (permalink / raw)
To: cygwin
CYGWIN_NT-6.1 HARRYR-PC 3.0.0(0.336/5/3) 2019-02-16 13:21 x86_64 Cygwin
GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)
#!/bin/bash
A="A"
B="A"
if [ $A!=$B ]; then
echo -e "not identical"
fi
if [ $A==$B ]; then
echo -e "identical"
fi
exit 0
Running this script gives
not identical
identical
Both tests are true.
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: bash string-operator problem
2019-02-21 16:10 bash string-operator problem Rockefeller, Harry
@ 2019-02-21 16:18 ` Eric Blake
2019-02-21 16:56 ` Lee
1 sibling, 0 replies; 7+ messages in thread
From: Eric Blake @ 2019-02-21 16:18 UTC (permalink / raw)
To: cygwin
On 2/21/19 10:00 AM, Rockefeller, Harry wrote:
> CYGWIN_NT-6.1 HARRYR-PC 3.0.0(0.336/5/3) 2019-02-16 13:21 x86_64 Cygwin
> GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)
>
> #!/bin/bash
> A="A"
> B="A"
> if [ $A!=$B ]; then
> echo -e "not identical"
> fi
> if [ $A==$B ]; then
> echo -e "identical"
> fi
> exit 0
>
> Running this script gives
> not identical
> identical
>
> Both tests are true.
Well, yeah, but it's not a bug in bash, but in your script. Whitespace
for argument separation is essential in [. You've provided exactly one
argument, and in one-argument mode, you are testing for non-empty
strings. Since both strings "A!=B" and "A==B" are non-empty strings,
the if clause is taken both times. You meant to provide three
arguments, and with proper quoting, and using the portable spelling of
equality (if you want to port your script to more than just bash):
[ "$A" != "$B" ]
[ "$A" = "$B" ]
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: bash string-operator problem
2019-02-21 16:10 bash string-operator problem Rockefeller, Harry
2019-02-21 16:18 ` Eric Blake
@ 2019-02-21 16:56 ` Lee
2019-02-21 19:39 ` john doe
1 sibling, 1 reply; 7+ messages in thread
From: Lee @ 2019-02-21 16:56 UTC (permalink / raw)
To: cygwin
On 2/21/19, Rockefeller, Harry wrote:
> CYGWIN_NT-6.1 HARRYR-PC 3.0.0(0.336/5/3) 2019-02-16 13:21 x86_64 Cygwin
> GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)
>
> #!/bin/bash
> A="A"
> B="A"
> if [ $A!=$B ]; then <----- needs spaces around the !=
> echo -e "not identical"
> fi
> if [ $A==$B ]; then <------ needs spaces around the ==
> echo -e "identical"
> fi
> exit 0
>
> Running this script gives
> not identical
> identical
>
> Both tests are true.
because you're testing "A==B"
you need to give 3 parameters "A" "==" "B"
$ cat x
#!/bin/bash
A="A"
B="A"
if [ $A != $B ]; then
echo "not identical"
fi
if [ $A == $B ]; then
echo "identical"
fi
if [ A!=B ]; then
echo "not identical"
fi
if [ A==B ]; then
echo "identical"
fi
Lee
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: bash string-operator problem
2019-02-21 16:56 ` Lee
@ 2019-02-21 19:39 ` john doe
2019-02-21 20:24 ` Lee
0 siblings, 1 reply; 7+ messages in thread
From: john doe @ 2019-02-21 19:39 UTC (permalink / raw)
To: cygwin
On 2/21/2019 5:18 PM, Lee wrote:
> On 2/21/19, Rockefeller, Harry wrote:
>> CYGWIN_NT-6.1 HARRYR-PC 3.0.0(0.336/5/3) 2019-02-16 13:21 x86_64 Cygwin
>> GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)
>>
>> #!/bin/bash
>> A="A"
>> B="A"
>> if [ $A!=$B ]; then <----- needs spaces around the !=
>> echo -e "not identical"
>> fi
>> if [ $A==$B ]; then <------ needs spaces around the ==
>> echo -e "identical"
>> fi
>> exit 0
>>
>> Running this script gives
>> not identical
>> identical
>>
>> Both tests are true.
>
> because you're testing "A==B"
> you need to give 3 parameters "A" "==" "B"
>
> $ cat x
> #!/bin/bash
> A="A"
> B="A"
> if [ $A != $B ]; then
> echo "not identical"
> fi
> if [ $A == $B ]; then
> echo "identical"
> fi
> if [ A!=B ]; then
> echo "not identical"
> fi
> if [ A==B ]; then
> echo "identical"
> fi
>
One equal sign ('=') should be used instead of two equal signs ('==').
--
John Doe
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: bash string-operator problem
2019-02-21 19:39 ` john doe
@ 2019-02-21 20:24 ` Lee
2019-02-21 21:46 ` Eric Blake
2019-02-21 22:26 ` Eliot Moss
0 siblings, 2 replies; 7+ messages in thread
From: Lee @ 2019-02-21 20:24 UTC (permalink / raw)
To: cygwin
On 2/21/19, john doe wrote:
> On 2/21/2019 5:18 PM, Lee wrote:
>> On 2/21/19, Rockefeller, Harry wrote:
>>> CYGWIN_NT-6.1 HARRYR-PC 3.0.0(0.336/5/3) 2019-02-16 13:21 x86_64 Cygwin
>>> GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)
>>>
>>> #!/bin/bash
>>> A="A"
>>> B="A"
>>> if [ $A!=$B ]; then <----- needs spaces around the !=
>>> echo -e "not identical"
>>> fi
>>> if [ $A==$B ]; then <------ needs spaces around the ==
>>> echo -e "identical"
>>> fi
<.. snip ..>
>
> One equal sign ('=') should be used instead of two equal signs ('==').
The man page for test says you're correct, but just out of curiosity
-- why do two equal signs work?
$ cat /tmp/x
#!/bin/bash
A="A"
B="A"
if [ $A == $B ]; then
echo "identical"
fi
if [ $A = $B ]; then
echo "identical"
fi
$ bash /tmp/x
identical
identical
Thanks,
Lee
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: bash string-operator problem
2019-02-21 20:24 ` Lee
@ 2019-02-21 21:46 ` Eric Blake
2019-02-21 22:26 ` Eliot Moss
1 sibling, 0 replies; 7+ messages in thread
From: Eric Blake @ 2019-02-21 21:46 UTC (permalink / raw)
To: cygwin
On 2/21/19 2:17 PM, Lee wrote:
>>
>> One equal sign ('=') should be used instead of two equal signs ('==').
>
> The man page for test says you're correct, but just out of curiosity
> -- why do two equal signs work?
Use of [ ... == ... ] is a bash extension. It works in bash, but is not
portable to other /bin/sh (notoriously == fails in dash, and is not
required by POSIX).
Oddly enough, POSIX is considering standardizing a common subset of [[;
in that case, [[ ... = ... ]] is non-portable, and the proposal only
documents [[ ... == ... ]] as being valid. For more than you ever
wanted to know, read http://austingroupbugs.net/view.php?id=375
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: bash string-operator problem
2019-02-21 20:24 ` Lee
2019-02-21 21:46 ` Eric Blake
@ 2019-02-21 22:26 ` Eliot Moss
1 sibling, 0 replies; 7+ messages in thread
From: Eliot Moss @ 2019-02-21 22:26 UTC (permalink / raw)
To: cygwin
On 2/21/2019 3:17 PM, Lee wrote:
> On 2/21/19, john doe wrote:
>> On 2/21/2019 5:18 PM, Lee wrote:
>>> On 2/21/19, Rockefeller, Harry wrote:
>>>> CYGWIN_NT-6.1 HARRYR-PC 3.0.0(0.336/5/3) 2019-02-16 13:21 x86_64 Cygwin
> $ cat /tmp/x
> #!/bin/bash
> A="A"
> B="A"
> if [ $A == $B ]; then
> echo "identical"
> fi
> if [ $A = $B ]; then
> echo "identical"
> fi
>
> $ bash /tmp/x
> identical
> identical
My reading of the bash man page says that = and == in this usage
do the same thing. However, = is Posix compliant, while (presumably)
== is not ... Eliot Moss
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2019-02-21 21:46 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-21 16:10 bash string-operator problem Rockefeller, Harry
2019-02-21 16:18 ` Eric Blake
2019-02-21 16:56 ` Lee
2019-02-21 19:39 ` john doe
2019-02-21 20:24 ` Lee
2019-02-21 21:46 ` Eric Blake
2019-02-21 22:26 ` Eliot Moss
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).