Discussion:
[Help-bash] Why does foo=bar compgen -e not show foo as a name?
Nick Chambers
2018-04-29 05:58:18 UTC
Permalink
Using bash 4.4, I am able to replicate this on both macos High Sierra
and Ubuntu 17.10. Why does the following not show foo as an
environment variable?

ubuntu:~ nchambers$ clear
ubuntu:~ nchambers$ foo=bar compgen -e
CC
CXX
EDITOR
HOME
LANG
LOGNAME
MAIL
PAGER
PATH
PS1
PWD
SHELL
SHLVL
TERM
USER
_system_arch
_system_name
_system_type
_system_version
ubuntu:~ nchambers$

I am not attempting to use this for anything practical, just happened
to notice it during a quick example I was writing.
David Margerison
2018-04-29 06:43:02 UTC
Permalink
Post by Nick Chambers
Using bash 4.4, I am able to replicate this on both macos High Sierra
and Ubuntu 17.10. Why does the following not show foo as an
environment variable?
ubuntu:~ nchambers$ clear
ubuntu:~ nchambers$ foo=bar compgen -e
$ type compgen
compgen is a shell builtin

so new no process is started, so compgen sees the same environment
as the shell.

Refer man bash COMMAND EXECUTION ENVIRONMENT which says

"When a simple command _other_than_a_builtin_ or shell function is to be
executed, it is invoked in a separate execution environment".
Chet Ramey
2018-04-29 22:15:41 UTC
Permalink
Post by Nick Chambers
Using bash 4.4, I am able to replicate this on both macos High Sierra
and Ubuntu 17.10. Why does the following not show foo as an
environment variable?
It's not exported to the environment, since `compgen' is a builtin command.

It doesn't really make sense for compgen to look in the temporary
environment anyway, since it's supposed to mimic, as much as is practical,
what a compspec constructed from a comparable set of completion options
would do when used for word completion. Word completion doesn't use the
temporary environment.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://tiswww.cwru.edu/~chet/
Loading...