How to list all variables names and their current values?
How to list all variables names and their current values? Including not only $HOME , $PWD etc but any other you have defined.
You’ve accepted an incorrect answer. «printenv» only gives you the environment variables. If you truly want all variables currently declared in your shell, use «declare -p» or «typeset -p».
11 Answers 11
For bash: (the standard shell in Ubuntu)
Enter the following command in a terminal to print all the environment variables:
For further information about this command, read the printenv man page.
To show a list including the «shell variables» you can enter the next command:
This will show you not only the shell variables, but the environment variables too.
For more information related with this topic read:
For zsh: (an advanced shell)
Use the following command:
( setopt posixbuiltin; set; ) | less
For more information about ZSH options, see zshoptions man page.
If I go to the terminal and write MYNEWVARIABLE=Ubuntu and execute printenv it doesn’t show there. Why is that, and how do those others show up?
Probably you are seeing the difference between a shell variable and an environment variable. Try export MYNEWVARIABLE=Ubuntu and it will work as you expect.
printenv is an external command, so it only knows about (and prints) exported environment variables. set is an internal bash command, so it shows all the «shell variables» (unexported environment variables) as well as the exported environment variables.
To expand on @Rmano’s reply to @Strapakowsky. This will not work unset MYNEWVARIABLE; MYNEWVARIABLE=Ubuntu; printenv | grep MYNEW , but this will unset MYNEWVARIABLE; export MYNEWVARIABLE=Ubuntu; printenv | grep MYNEW , and this will unset MYNEWVARIABLE; MYNEWVARIABLE=Ubuntu printenv | grep MYNEW . Using export says «the variable I’m setting should be part of the environment that gets passed to processes, not just a variable in this shell.» My third example says «the variable should be part of the environment that gets passed to THIS process, but not stick around afterward.»
You can see all variables with the declare builtin.
If you’re only interested in environment variables, use
Run help declare to see what the other options are.
this is far neat-er solution than POSIXLY_CORRECT=1 set and it is also worthy of mention that declare is alias (in that context) for typeset , another bash builtin.
@maoizm If you want only the variable names and nothing else, it’s easier to use compgen for that. compgen -e .
I know that this question is quite old and answered, but I think I can add a bit of useful information.
In all the methods described above, the procedure that is suggested is:
The problem of these solutions are that you are seeing the environment variables of the shell that is running into the terminal. You are not seeing the environment variables available to an application run, for example, directly by the graphic interface.
This is noticeable if, for example, you use your ~/.profile , or .bashrc , or .zshenv (depending on your shell) to modify the environment variables — like the classic addition of directories to the path.
To see the environment variables available to the application started directly in the graphic environment, you can do the following (in Gnome Shell, I am sure there is an equivalent method in all the other DE):
(Or, if you do not have xterm , gnome-terminal — bash —noprofile —norc — thanks to @Mike Nakis for the comment).
You now have a terminal with a shell that did not add any environment variables. You can use env here to list all your environment variables:
Obviously the new shell will have the environment variables added by the system files, but that variables should be available (by inheritance) to all programs in the system anyway.
I am posting this because it’s the fourth time I have to search this trick again, checking my .pam_environment file. So now I will find it faster (and in the process, I hope helping someone else. )
How to print / echo environment variables?
The expansion $NAME to empty string is done by the shell earlier, before running echo , so at the time the NAME variable is passed to the echo command’s environment, the expansion is already done (to null string).
To get the same result in one command:
Note that only the printenv -based command preserves the semantics of the OP’s command: defining NAME as a command-scoped environment variable, which only the invoked command and its child processes see, but no subsequent shell commands. The other commands do something very different: they define NAME as an until-the-current-shell-exits shell-only variable, which all subsequent shell commands see, but no external utilities.
To bring the existing answers together with an important clarification:
As stated, the problem with NAME=sam echo «$NAME» is that $NAME gets expanded by the current shell before assignment NAME=sam takes effect.
Solutions that preserve the original semantics (of the (ineffective) solution attempt NAME=sam echo «$NAME» ):
Use either eval [1] (as in the question itself), or printenv (as added by Aaron McDaid to heemayl’s answer), or bash -c (from Ljm Dullaart’s answer), in descending order of efficiency:
NAME=sam eval 'echo "$NAME"' # use `eval` only if you fully control the command string NAME=sam printenv NAME NAME=sam bash -c 'echo "$NAME"'
printenv is not a POSIX utility, but it is available on both Linux and macOS/BSD.
What this style of invocation ( = cmd . ) does is to define NAME :
- as an environment variable
- that is only defined for the command being invoked.
In other words: NAME only exists for the command (child process) being invoked, and has no effect on the current shell (if no variable named NAME existed before, there will be none after; a preexisting NAME variable remains unchanged).
POSIX defines the rules for this kind of invocation in its Command Search and Execution chapter.
The following solutions work very differently (quoted from heemayl’s answer):
NAME=sam; echo "$NAME" NAME=sam && echo "$NAME"
While they produce the same output, they instead define:
- a shell variable NAME (only) rather than an environment variable
- if echo were a command that relied on environment variable NAME , it wouldn’t be defined (or potentially defined differently from earlier).
Note that every environment variable is also exposed as a shell variable, but the inverse is not true: shell variables are only visible to the current shell and its subshells, but not to child processes, such as external utilities and (non-sourced) scripts (unless shell variables are designated as environment variables (too) with export or declare -x ).
[1] Technically, bash is in violation of POSIX here (as is zsh ): Since eval is a special shell built-in, the preceding NAME=sam assignment should cause the the variable $NAME to remain in scope after the command finishes, but that’s not what happens.
However, when you run bash in POSIX compatibility mode, it is compliant.
dash and ksh are always compliant.
The exact rules are complicated, and some aspects are left up to the implementations to decide; again, see Command Search and Execution.
Also, the usual disclaimer applies: Use eval only on input you fully control or implicitly trust.How to list all Linux environment variables including LD_LIBRARY_PATH
How to list all the environment variables in Linux? When I type the command env or printenv it gives me lots of variables, but some variables like LD_LIBRARY_PATH and PKG_CONFIG don’t show up in this list. I want to type a command that list all the environment variables including this variables ( LD_LIBRARY_PATH and PKG_CONFIG )
3 Answers 3
env does list all environment variables.
If LD_LIBRARY_PATH is not there, then that variable was not declared; or was declared but not export ed, so that child processes do not inherit it.
If you are setting LD_LIBRARY_PATH in your shell start-up files, like .bash_profile or .bashrc make sure it is exported:
export LD_LIBRARY_PATH=/usr/local/lib:$
This will modify the variable.
To print it, type: echo $LD_LIBRARY_PATH and it should show the above value.
If you’re seeing nothing when you print that, then the variable might not be set.
The question in fact is a good question. when run env or printenv , the output will be the system environment, but LD_LIBRARY_PATH is not belong to.
For example, if you set a=1 , you can’t show it by env . Same as LD_LIBRARY_PATH, it is used by ld.so only(ld. so – this little program that starts all your applications)
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.