Re: [Help-bash] Solved: Re: Distinguish between unset and empty variable
From:
Chet Ramey
Subject:
Re: [Help-bash] Solved: Re: Distinguish between unset and empty variables in loop.
Date:
Thu, 15 Dec 2016 14:53:56 -0500
User-agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.5.1
On 12/15/16 2:47 PM, Christof Warlich wrote:
> Am 15.12.2016 um 03:36 schrieb Dmitry Alexandrov:
>> ,----[ ./warlich ]
>> | #!/bin/bash
>> |
>> | xxx=hi
>> | yyy=""
>> | unset zzz
>> |
>> | for v in xxx yyy zzz; do
>> | [[ -v $v ]] || printf -v "$v" 'default'
>> | printf '%s=%s\n' "$v" "${!v}"
>> | done
>> `----
>>
>> ,----
>> | $ ./warlich
>> | xxx=hi
>> | yyy=
>> | zzz=default
>> `----
> That's definitly much better than my hack, thanks :-).
>
> I would just not have expected that the "unset-ness" of a variable is still
> available directly through the loops index variable ...
The -v operator takes a variable name; $v expands to the desired variable
name.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU address@hidden http://cnswww.cns.cwru.edu/~chet/