List env vars linux

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.

Читайте также:  Install apache oracle linux

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.

Читайте также:  Выдать права sudo пользователю linux

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:

Example of the bare shell

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 do I see a list of all currently defined environment variables in a Linux bash terminal?

In a Linux bash terminal, there are often many environment variables that have been set, like $PATH and $HOME . Is it possible to see all of the environment variables that have been set? How?

5 Answers 5

TL;DR: use (set -o posix ; set)

According to the Bash manual you can use the set built-in command to show all of the environment variables that have been set. The set command will also display the definitions of any functions. If you only want to see the variables, and not the functions, then you can turn on POSIX mode before running the set command. The easiest way to do that is with set -o posix , but that will leave POSIX mode on until you turn it off with set +o posix .

Читайте также:  Файлы драйверов в линукс

Therefore, the following command will show all of the defined environment variables by using a subshell without affecting POSIX compliance in your current shell.

@RedGrittyBrick and @iglvzx suggested using the env command, however this command will not provide a complete listing of environment variables. env will only show the varaibles that have been marked for export. Compare the output of env | sort and export -p and you will see what I mean. You can run comm -23 <(set -o posix; set) <(env|sort) if you want to see which environment variables are not being exported.

The reason for the discrepancy is that env is a separate executable as opposed to set which is a shell built-in command. According to the Bash manual, when a command is executed that is not a shell built-in command or function it will only receive environment variables that have been marked for export in Bash. There are many variables that are not exported. Therefore, if you wish to see all of the variables that your shell has defined, you must use the set command as stated in the manual.

You can easily test this behavior for yourself using the following commands.

MY_TEST_VARIABLE="This is my test variable." set | grep MY_TEST_VARIABLE env | grep MY_TEST_VARIABLE 

You will see that set provides output while env does not.

Источник

Оцените статью
Adblock
detector