help-bash
[Top][All Lists]
Advanced

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

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


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

Hi,
admittedly, the subject sounds a bit strange, but the issue will soon become clear: I want to set default values to a list of variables if they are unset, but leave them untouched otherwise. I want to do this in a loop, because the list of variables is rather big. But so far, I cannot distinguish between empty and unset variables. Here is a stripped-down example what I have:
xxx=hi:
yyy="";
for i in xxx yyy zzz; do
[ -z ${!i} ] && eval "$i=default"; echo $i=${!i};
done
Running this yields:
xxx=hi
yyy=default
zzz=default
whereas I’d like to get the result below because yyy was previously set to the empty string:
xxx=hi
yyy=
zzz=default
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.
Anyone having a idea how this could be done?
Thanks,
Chris



reply via email to

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