Christof Warlich
2016-11-23 16:59:36 UTC
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
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