Using bash 4.3, there seems to be a discrepancy between the manual and actual behaviour:
unset string # just to be sure
declare string # $? is 0 afterwards
declare -p string # fails, 'string: not found'
printf %b "${string-unset}" "\n" # consequently, yields unset
The manual segment for the declare/typeset builtin does not state that an assignment upon declaration is required. On the contrary, the declare [...] [...] name[=value]
notation hints that this should be legal.
What am I missing, is it a definition thing of what a variable being "set" is supposed to mean? Have I misread the "Parameter Expansion" segment of the manual, which at least uses the term "unset"? Or is this just a version-specific oddity?
declare
without any options also does not seem to contain the name "string". – ithaca Jan 10 '19 at 18:59declare
might be erroneous. – ithaca Jan 10 '19 at 19:01declare -p string
does not work for you? As soon asdeclare -p
is executed on an assigned-to name, this seems ok:unset x; declare x=2; declare -p x
. – ithaca Jan 10 '19 at 19:04declare -p
asdeclare -- string
– jesse_b Jan 10 '19 at 19:07string=
you get the desired result from${string-unset}
, but will notice declare -p showsdeclare -- string=""
. Not really sure if that's relevant. – jesse_b Jan 10 '19 at 19:27