Hi guys. I cannot wrap my hear around this: -> $ unset _Val; test -z ${_Val}; echo $? 0 -> $ unset _Val; test -n ${_Val}; echo $? 0 -> $ _Val=some; test -n ${_Val}; echo $? 0 What is this!? How should two different, opposite tests give the same result Is there some bash option which affects that and if so, then what would be the purpose of such nonsense? many thanks, L.
Hello lejeczek, On Wed, 19 Apr 2023 07:50:29 +0200 lejeczek via CentOS <centos at centos.org> wrote:> Hi guys. > > I cannot wrap my hear around this: > > -> $ unset _Val; test -z ${_Val}; echo $? > 0 > -> $ unset _Val; test -n ${_Val}; echo $? > 0 > -> $ _Val=some; test -n ${_Val}; echo $? > 0 > > What is this!? > How should two different, opposite tests give the same result > Is there some bash option which affects that and if so, then what would be the purpose of such nonsense?Surround ${_Val} with double quotes (as you should) and things will be different: $ unset _Val; test -n "${_Val}"; echo $? 1 Now you get it? :-) Regards, -- wwp -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 836 bytes Desc: OpenPGP digital signature URL: <http://lists.centos.org/pipermail/centos/attachments/20230419/1881eebd/attachment-0002.sig>
On 19/04/2023 08:04, wwp wrote:> Hello lejeczek, > > > On Wed, 19 Apr 2023 07:50:29 +0200 lejeczek via CentOS <centos at centos.org> wrote: > >> Hi guys. >> >> I cannot wrap my hear around this: >> >> -> $ unset _Val; test -z ${_Val}; echo $? >> 0 >> -> $ unset _Val; test -n ${_Val}; echo $? >> 0 >> -> $ _Val=some; test -n ${_Val}; echo $? >> 0 >> >> What is this!? >> How should two different, opposite tests give the same result >> Is there some bash option which affects that and if so, then what would be the purpose of such nonsense? > Surround ${_Val} with double quotes (as you should) and things will be different: > > $ unset _Val; test -n "${_Val}"; echo $? > 1 > > Now you get it? :-) > >I don't know, am not sure, I remembered it differently, did not think enclosing quotes were necessary(always?) for that were {} thanks, L.
On Wed, Apr 19, 2023 at 07:50:29AM +0200, lejeczek via CentOS wrote:>Hi guys. > >I cannot wrap my hear around this: > >-> $ unset _Val; test -z ${_Val}; echo $? >0 >-> $ unset _Val; test -n ${_Val}; echo $? >0 >-> $ _Val=some; test -n ${_Val}; echo $? >0 > >What is this!? >How should two different, opposite tests give the same result >Is there some bash option which affects that and if so, then >what would be the purpose of such nonsense? > >many thanks, L.Quoted $_Val expands to a null, zero length string. Unquoted $_Val expands to nothing which is different. An alternative test with double square brackets would give you your expected results as it automatically quotes unquoted variables. $ unset _Val ; [[ -n $_Val ]] ; echo $? jl -- Jon H. LaBadie jcu at labadie.us