I'd like to echo
all non-environment variables (all self-declared variables), in Bash 3.2.52.
This command to print all variables gave me output I can't understand that seems to me to conflict with the set -x && clear -r
mode I already work in.
diff -U 1 <(set -o posix ; set |cut -d= -f1) <(
exec bash -ic 'set -o posix ; set' | cut -d= -f1) |
grep '^[-][^-]' |
cut -d- -f2 |
grep -vE '^(COLUMNS|HISTFILESIZE|HISTSIZE|LINES|PIPESTATUS)$'
I need an echo
or printf
(or any other "simpler") operation to have such list.
If possible in this version of Bash, how can this be achieved?
Best Answer
GNU Parallel includes
env_parallel
. Part ofenv_parallel
lists all variables for the supported shells.For
bash
this code is:So given all variables we need to figure out which ones are set by the user. We can do that by hardcoding all variables set by a given version of
bash
, but that would not be very future proof, becausebash
may set more variables in future versions (Is$BASH_MYVAR
set by the user or by a future version ofbash
?).So instead
env_parallel
asks you to define a clean environment, and runenv_parallel --session
in that. This will set$PARALLEL_IGNORED_NAMES
by listing the names defined before (using the code above).When later run in an environment where the user has set variables, it is easy to take the difference between the clean environment and the current environment.
env_parallel --session
makes it possible to define a clean environment for every session, but if you prefer to have a reference environment that can be used across sessions, simply save the list of variables to a file. So:For example: