I want to get a list of all environment variables (shell variables? exported variables?) and their values at a given time, in zsh.
What is the proper way to do this?
2 Answers
It sounds like you want env.
-
5This doesn't work for me. The
HOSTenv is missing from the output ofenv. On zsh I was able to list everything withset.Chris– Chris2021-12-20 03:14:55 +00:00Commented Dec 20, 2021 at 3:14 -
1@Chris If
$HOSTis missing from env, then it's not an environment variable.Chris Down– Chris Down2021-12-22 17:18:09 +00:00Commented Dec 22, 2021 at 17:18 -
2@ Chris (nice name!). Where's
echo $HOSTcoming from? Is this some sort of zsh variable that's not an actual OS environment variable? For my purposes$HOSTexists and has the value I want, but it only appears insetand notenv.Chris– Chris2022-01-06 23:27:48 +00:00Commented Jan 6, 2022 at 23:27 -
3@Chris
$HOSTis a zsh variable instantiated on startup which is not exported by default. Most variables are not environment variables unless you explicitlyexportthem. See zsh.sourceforge.io/Doc/Release/Parameters.html#index-HOSTChris Down– Chris Down2022-01-07 19:04:13 +00:00Commented Jan 7, 2022 at 19:04
export prints out the list of environment variables and their values. The values are quoted, the output of export is suitable for reading back into the shell. The variables are printed in alphabetical order.
If you want shell variables as well, use set. If you want shell variables with type annotations (exported, integer, etc.), use typeset.
You can use export and set on other shells as well, but most don't quote the output, so it's not parseable. typeset is available (with different behavior) on ksh and bash.
If you want the environment variables, there's also the env command, which prints unsorted, unquoted
If you only want the names, access the parameters associative array. The keys are the parameter names and the values indicate the types.
-
Some difference with
env:env -i '$(reboot)=1' zsh -c export(compare withbashwhere you don't want to feed it back into the shell (fixed in 4.4)). Andenv -i zsh 'export foo; export'. See also zsh.org/mla/workers/2016/msg01840.html and theenv -i zsh -c exportbug in recent versions of zsh which I've just reported.Stéphane Chazelas– Stéphane Chazelas2016-09-30 09:09:03 +00:00Commented Sep 30, 2016 at 9:09 -
2See also
typeset -p +H -m '*'to list all variables, including hidden ones.Stéphane Chazelas– Stéphane Chazelas2016-09-30 09:15:03 +00:00Commented Sep 30, 2016 at 9:15
typeset -xor equivalentlyexport, or/usr/bin/env(or justenvif your$PATHis set right, i.e contains/usr/bin:)zshshell, I would recommend installing these plugins:alias-finder,aliases. To install append the mentioned plugin names in~/.zshrcto the line describing plugins.envandexportare alsoe good options when combined withgreplike thisexport | grep "git"