Skip to main content
11 events
when toggle format what by license comment
Jun 2, 2023 at 13:12 comment added Stéphane Chazelas Note that declared -p alone 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 -F is meant to list functions without their definitions (poor choice when -F is for floating point variables in other shells). If you look at the source, what happens with -p +F is that the + turns on nodefs, but also means the fact that we ask for functions is recorded for flags_off, so flags_on remains unset, so with -p we end up listing variable attributes with nodefs set and that nodefs flag 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 foo in that case, right? It just "shadows" the environment foo.
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 does declare -i foo=42 while foo is already in the environment, you get declare -ix foo="42". Unless the script goes through the bother to unset foo first.
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 grep or sed you can of course also use bash-builtin for to go through the lines, and use test / [ 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 GNU grep but with GNU bash, 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 grep or sed are 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 the declare -p output? It would allow a Bash-only solution ...
Jun 1, 2023 at 12:31 history answered Marcus Müller CC BY-SA 4.0