bug-bash
[Top][All Lists]
Advanced

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

Re: declare -x non-exportable variable types


From: Chet Ramey
Subject: Re: declare -x non-exportable variable types
Date: Fri, 25 Feb 2022 10:49:10 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.6.0

On 2/22/22 1:35 PM, Léa Gris wrote:
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"

You can't export attributes, since the only vehicle to export objects to
child processes is environment variables. In this case, the variable was
found in the environment at shell startup, so it was given the export
attribute in the child shell. That is standard behavior, defined by POSIX.


$ 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

You can't export array variables, period.


$ 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"

You can't export attributes, only variable names and values.


$ unset str && unset -n ref && declare str=hello && declare -nx ref=str && bash -c 'declare -p ref'
declare -x ref="str"

You told declare to act on the nameref variable (-n) and export it (-x).
You still can't export attributes.

The inconsistency is that sometimes:
- It exports the variable with its translated value (integer to string, upper, lower)

Because these attributes result in behavior at assignment time. The
converted value is what is stored and returned when the variable is
referenced. The original value used in the assignment statement is
discarded after being converted.

- 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.

Bash doesn't export array variables. There is code to do it in there, and
has been for a long time, but it's not enabled.

- It export the reference name of nameref, despite it could export the value of the referee.

If you want to export the referenced variable, don't tell declare to
operate on the name reference instead. Something like

unset str && unset -n ref && declare str=hello && declare -n ref=str && declare -x ref
./bash -c 'declare -p str ref'

would easily do what you want.


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.

There is no way to export attributes, period, regardless of whether they
are `incompatible with export.'

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.

There are no flags that are compatible or incompatible with export.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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