Web lists-archives.com

Re: bash string-operator problem




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