declare -x variable with unexportable flag/type is handled quite
inconsistently:
$ unset int_value && declare -ix int_value=42 && bash -c 'declare -p
int_value'
declare -x int_value="42"
$ unset array && declare -ax array=(foo bar) && bash -c 'declare -p array'
bash: line 1: declare: array: not found
$ unset assoc && declare -Ax assoc=([key1]=foo [key2]=bar) && bash -c
'declare -p assoc'
bash: line 1: declare: assoc: not found
$ unset upper && declare -ux upper='hello' && bash -c 'declare -p upper'
declare -x upper="HELLO"
$ unset lower && declare -lx lower='WORLD' && bash -c 'declare -p lower'
declare -x lower="world"
$ unset str && unset -n ref && declare str=hello && declare -nx ref=str
&& bash -c 'declare -p ref'
declare -x ref="str"
The inconsistency is that sometimes:
- It exports the variable with its translated value (integer to string,
upper, lower)
- It exports nothing of array or associative array, despite that in
Bash, array referenced without an index returns the first element, but
not when exported.
- It export the reference name of nameref, despite it could export the
value of the referee.
My stance on this is that there is no real consistent way to export
variables with incompatible flags; and I wonder if to be consistent, the
declaration statement could be erroring instead, when the other variable
flag are incompatible with export.
For the built-in export or system export command, a consistent
conversion of the value sounds an acceptable behavior:
export int_value # export with string of value
export array # export with value of first elmeent
export assoc # export empty value (same as referencing an associative
array without a [key].
export upper; export lower # export with converted value
export nameref # export with value of the referee
This means that the export command would expand the variable value
before exporting, but the declare, local and typeset statements would
error if flags are incompatible with -x export.
--
Léa Gris