help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Distinguish between unset and empty variables in loop.


From: Christof Warlich
Subject: Re: [Help-bash] Distinguish between unset and empty variables in loop.
Date: Wed, 23 Nov 2016 20:35:33 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0


Greg Wooledge wrote:

You can use the ${parameter+word} expansion for this.

imadev:~$ x=''; unset y
imadev:~$ echo "${x+hello}"
hello
imadev:~$ echo "${y+hello}"

imadev:~$

Well, that's been my major issue: This can't work from within the loop:

Christof Warlich wrote:

The usual procedures to distinguish between unset and empty
variables   (i.e. [ -z ${var+x} ] or [[ -v var ]]) do not work
here, as the loop variable is always set.

Please revisit my example, taking the for-loop into account:

        xxx=hi:
        yyy="";
        for i in xxx yyy zzz; do
                [ -z ${!i} ] && eval "$i=default"; echo $i=${!i};
        done

I can't see how to apply the ${var+x} (or ${x+hello}} pattern to this situation.

If I'm really missing something here, could you just modify the loop
so that it prints

        xxx=hi
        yyy=
        zzz=default

instead of

        xxx=hi
        yyy=default
        zzz=default

? Again, thanks for your help,

Chris





reply via email to

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