bug-bash
[Top][All Lists]
Advanced

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

local -p outputs invalid declare -- -


From: Emanuele Torre
Subject: local -p outputs invalid declare -- -
Date: Fri, 1 Jul 2022 07:16:54 +0200

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -march=x86-64 -mtune=generic -O2 -pipe -fno-plt
-DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/bin'
-DSTANDARD_UTILS_PATH='/usr/bin' -DSYS_BASHRC='/etc/bash.bashrc'
-DSYS_BASH_LOGOUT='/etc/bash.bash_logout'
-DNON_INTERACTIVE_LOGIN_SHELLS
uname output: Linux t420 5.15.50-1-lts #1 SMP Sat, 25 Jun 2022
14:58:59 +0000 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.1
Patch Level: 16
Release Status: release

Description:
        `local -p' (and also `declare -p'/`typeset -p') outputs
        "declare -- -" when used in a function that has declared
        `local -'.

        `declare -' (and `typeset -') is not a valid command, not even
        in functions.
        The only valid way to localise shell options is `local -'

Repeat-By:
          bash-5.1$ declare -
          bash: declare: `-': not a valid identifier
          bash-5.1$ a () { declare -- -; local -p ;}
          bash-5.1$ a
          bash: declare: `-': not a valid identifier

          bash-5.1$ local -
          bash: local: can only be used in a function
          bash-5.1$ a () { local -; local -p ;}
          bash-5.1$ a
          declare -- -

Fix:
        One solution could be to print `local -' instead of
        `declare -- -' when shell options are localised, so that the
        output of local/declare/typeset -p can correctly be evaluated by
        function to restore variables and `local -'.

        Another alternative solution is to allow `declare -'/`typeset -'
        in functions, since they are usually equivalent to `local'
        anyway (except for some uses like `local -p') and I don't see
        the point of only allowing `local -'.

        Also, if bash allows `declare -', it could implement it as a
        noop if it is ran outside of a funciton and as `local -' if ran
        inside a function. These way files generated by
        `declare -p > file' could be safely sourced by both functions
        and non-functions without triggering the "declare: `-': not a
        valid identifier" error message.



reply via email to

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