[Top][All Lists]

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

Re: local/typeset/declare -p - outputs invalid declare -- -

From: Emanuele Torre
Subject: Re: local/typeset/declare -p - outputs invalid declare -- -
Date: Wed, 16 Nov 2022 21:00:22 +0100

On Wed, Nov 16, 2022 at 02:05:56PM -0500, Chet Ramey wrote:
> It doesn't matter. `local -' is the only way to save and restore the
> options; `declare -p -' and `typeset -p -' should have no special meaning.

The `declare -p'/`typeset -p' commands and the `local -p' command are
different commands.
The former gets the variable (or "-") from the first variable scope that
can be seen from the function, and the latter only gets variables (or
"-") that are declared in the scope of the current function.

    a () { printf 'LOCAL(a)\n'; local   -p -- "$@";
           printf 'DECLARE\n';  declare -p -- "$@"; }
    b () { local x=2; printf 'LOCAL(b)\n'; local -p x; a x ;}
    c () { local -;   printf 'LOCAL(c)\n'; local -p x; a - ;}

    # LOCAL(b)
    # declare -- x="2"
    # LOCAL(a)
    # bash: local: x: not found
    # declare -- x="2"
    # LOCAL(c)
    # local -
    # LOCAL(a)
    # bash: local: -: not found
    # declare -- -

The are different commands with different meanings.

I don't agree that `declare -p -' should not be meaningful: it tells you
whether `local -' is declared in the current scope OR the scope of one
of the callers.

I don't see why a built-in that is supposed to output valid code, should
be allowed to output invalid code only in this specific case, and should
require the user to use

    declare -p - 2> /dev/null && printf 'local -\n';

or something more complex if `declare -p -- "${tosave[@]}"' is used
instead of just outputting valid code in all cases.


reply via email to

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