Re: bash test ?

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



On Wed, Apr 19, 2023 at 09:16:26PM +0200, lejeczek via CentOS wrote:
On 19/04/2023 08:46, wwp wrote:
Hello lejeczek,
...
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 {}
{} does not prevent this (at least not in bash):

$ FOO="a b"

$ test -z $FOO
bash: test: a: binary operator expected

$ test -z ${FOO}
bash: test: a: binary operator expected

Because after $FOO or ${FOO} variable expansion, bash parsed:
test -z a b
'b' is unexpected, from a grammar point of view.

Quoting is expected, here:
$ test -z "$FOO"
<no error>

When FOO is unset, apparently it's a different matter, where you end up
with $?=0 in all unquoted -n/-z cases, interestingly. I could not find
this specific case in the bash documentation. That may not be portable
to other shells, BTW. I only use {} when necessary (because of what
bash allows to do between {}, plenty!, or when inserting $FOO into a
literal string that may lead the parser to take the whole string for a
variable name: echo $FOObar != echo ${FOO}bar).


Regards,
There is a several ways to run tests in shell, but 'test'
which is own binary as I understand, defeats me..

Yes, there is a binary for test (and its alternate '[').

But most shells, including bash have incorporated code for test
(and other commands) into the shell code itself for efficiency.

$ type test
test is a shell builtin


I'd expect a consistency, like with what I usually do to
test for empty var:
-> $ export _Val=some; [[ -v _Val ]]; echo $?
0
-> $ unset _Val; [[ -v _Val ]]; echo $?
1


I do hope you don't use -v to test for empty variables as
it tests for "set" variables and valid name syntax.

Set variables can be "empty"  ( name= ).

But in your last example _Val is "un"set, it does not
exist.  Thus it can neither be empty nor occupied.

--
 Jon H. LaBadie                  jcu@xxxxxxxxxx
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
https://lists.centos.org/mailman/listinfo/centos



[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]


  Powered by Linux