Show env var 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.

Читайте также:  Linux не работает telnet

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.

Читайте также:  How to make linux installer

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. )

Источник

What are my environment variables? [closed]

Wow that was fast! I guess all the command do the trick. The export command gave me a lot of «declare -x» in front. Thanks guys!

It was inappropriate to close this question as off topic. When programming on Linux, as I am doing at the moment, it is often useful to discover what the environmental variables are. Quite a lot of people have found this to be a useful question, including me.

4 Answers 4

I am not sure if thats what you want, but try printenv
This will show you all your environment variables.

Just execute env in a terminal.

$ env TERM=xterm SHELL=/bin/bash USER=joksnet USERNAME=joksnet DESKTOP_SESSION=gnome PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games PWD=/home/joksnet GDM_KEYBOARD_LAYOUT=us LANG=en_US.utf8 HOME=/home/joksnet DISPLAY=:0.0 COLORTERM=gnome-terminal _=/usr/bin/env 

Type export without any parameters.

SET(P) POSIX Programmer’s Manual SET(P) NAME set - set or unset options and positional parameters SYNOPSIS set [-abCefmnuvx][-h][-o option][argument. ] set [+abCefmnuvx][+h][+o option][argument. ] set -- [argument. ] set -o set +o DESCRIPTION If no options or arguments are specified, set shall write the names and values of all shell variables in the collation sequence of the current locale. Each name shall start on a separate line, using the format: "%s=%s\n", , The value string shall be written with appropriate quoting; see the description of shell quoting in Quoting . The output shall be suitable for reinput to the shell, setting or resetting, as far as possible, the variables that are currently set; read-only variables cannot be reset. 

env or printenv are better. In bash, set will also print all your defined functions, which on a system like ubuntu, is a very long printout.

Читайте также:  Linux list all env variables

Источник

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