Timeline for Concise way to get only global and local shell variables (not functions or env. variables) in Bash
Current License: CC BY-SA 4.0
        11 events
    
    | when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 2, 2023 at 13:12 | comment | added | Stéphane Chazelas | Note that declared -palone excludes functions already. | |
| Jun 2, 2023 at 13:12 | comment | added | Stéphane Chazelas | Note that it's relying on undocumented and likely unintended behaviour. declare -Fis meant to list functions without their definitions (poor choice when-Fis for floating point variables in other shells). If you look at the source, what happens with-p +Fis that the+turns onnodefs, but also means the fact that we ask for functions is recorded forflags_off, soflags_onremains unset, so with-pwe end up listing variable attributes withnodefsset and thatnodefsflag seems to be honoured for some types of variables and not others. | |
| Jun 1, 2023 at 15:56 | vote | accept | 0xC0000022L | ||
| Jun 1, 2023 at 15:56 | comment | added | 0xC0000022L | @MarcusMüller it's a fair point that we don't know about the future, but I was hoping that I missed some sort of documentation where this format is specified or so. | |
| Jun 1, 2023 at 14:01 | comment | added | ilkkachu | @MarcusMüller, right, I don't know what they'd like to do in that case. I guess there's the inverse case too, where any normal variable could get the export attribute if it was set in the original environment, and then get ignored by filter. Unless the script unsets it, of course. Sh-like shells don't really keep envvars and shell-internal vars that separate. Sure, likely not such an issue, but, anyway. | |
| Jun 1, 2023 at 13:55 | comment | added | Marcus Müller | @ilkkachu But I think that's fine, right? OP asks for fooin that case, right? It just "shadows" the environmentfoo. | |
| Jun 1, 2023 at 13:54 | comment | added | ilkkachu | note that the output could also be e.g. declare -irx .... For variables that were only imported from the environment and not touched in the script you'd only see-x, but if the script doesdeclare -i foo=42whilefoois already in the environment, you getdeclare -ix foo="42". Unless the script goes through the bother tounset foofirst. | |
| Jun 1, 2023 at 12:53 | comment | added | Marcus Müller | Note that you asked for the most concise solution, and any bash-only solution is going to be less concise ;) | |
| Jun 1, 2023 at 12:52 | comment | added | Marcus Müller | Nobody can know how the bash developers plan to develop bash for the next 30 years, but as far as I can tell, that format has never fundamentally changed (nor would it make sense for it to do). Instead of greporsedyou can of course also use bash-builtinforto go through the lines, and usetest/[to test for string pattern matching. That's up to you to develop, though. If you need a payload to work on a machine without GNUgrepbut with GNUbash, the general applicability of every proposed solution is basically null, so I'll let you develop that part of the solution yourself | |
| Jun 1, 2023 at 12:45 | comment | added | 0xC0000022L | Thanks for the answer, this trick might help to remove functions and by extension other stuff. Although neither is "in Bash", given greporsedare used as external tools (but my wording may have been the issue). I have my reasons why I want to avoid external tools in this particular case, even though you may not deem my reasons valid. Also, it's odd that this doesn't seem to work to exclude exported variables, e.g. with+x. Seems inconsistent (as per the comment by Kusalananda). Do you know if I can rely on the format of thedeclare -poutput? It would allow a Bash-only solution ... | |
| Jun 1, 2023 at 12:31 | history | answered | Marcus Müller | CC BY-SA 4.0 |