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. )
check if environment variable is already set [duplicate]
I am writing a shell script, where I have to check if environment variable is set, if not set then I have to set it. Is there any way to check in shell script, whether an environment variable is already set or not ?
Actually, this is not a true duplicate of that question: The OP here wants to know whether a variable was set, and if not do something about it, instead of exitting a script. $ is not a valid answer in that case. set | grep -q ‘^V=’ is. This tests for the presence of the variable and works even if the variable was set but to the empty string.
4 Answers 4
The standard solution to conditionally assign a variable (whether in the environment or not) is:
That will set VAR to the value «foo» only if it is unset.
To set VAR to «foo» if VAR is unset or the empty string, use:
To put VAR in the environment, follow up with:
You can also do export VAR=$ or export VAR=$ , but some older shells do not support the syntax of assignment and export in the same line. Also, DRY; using the name on both sides of the = operator is unnecessary repetition. (A second line exporting the variable violates the same principal, but feels better.)
Note that it is very difficult in general to determine if a variable is in the environment. Parsing the output of env will not work. Consider:
export foo=' VAR=var-value' env | grep VAR
Nor does it work to spawn a subshell and test:
That would indicate the VAR is set in the subshell, which would be an indicator that VAR is in the environment of the current process, but it may simply be that VAR is set in the initialization of the subshell. Functionally, however, the result is the same as if VAR is in the environment. Fortunately, you do not usually care if VAR is in the environment or not. If you need it there, put it there. If you need it out, take it out.
How to check if an environment variable exists and get its value? [duplicate]
I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn’t have to be present. For instance, assume, before running the script, I perform the following operation:
How do I tell the script to search for this environment variable, and store its value in a variable inside the script. Moreover, how do I tell the script that if this environment variable does not exist, store a default variable?
I tried, for testing purposes tempV=$(printenv LANG) it did not print anything, despite the system having the value of LANG
Your statement doesn’t «print» anything. It stores the value of the environment variable LANG into the shell variable tempV . As for the other suggestions in this thread: Note that with none of them, you can distinguish, whether you have an environment variable or a shell variable. If this distinction is really important, the solution suggested by @sjsam should be considered.
5 Answers 5
[ -z «$» ] checks whether DEPLOY_ENV has length equal to zero. So you could run:
if [[ -z "$" ]]; then MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined" else MY_SCRIPT_VARIABLE="$" fi # or using a short-hand version [[ -z "$" ]] && MyVar='default' || MyVar="$" # or even shorter use MyVar="$"
MyVar=»$DEPLOY_ENV:-default_value» didn’t work for me though on bash, so it’s generally safer to surround with curly braces $ <. >where possible.
This works and may have value in edge cases, but it’s a re-invented wheel. Instead, see the parameter «expansion answer», which was made for this exact goal.
$
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
There’s also the $ form, which substitutes the default value only when parameter is unset (but not when it’s null).
To demonstrate the difference between the two:
$ unset DEPLOY_ENV $ echo "'$' '$'" 'default_value' 'default_value' $ DEPLOY_ENV= $ echo "'$' '$'" 'default_value' ''
My script was not able to read the value of DEPLOY_ENV, In order to be sure that the environment variable exists in the system, I wrote printenv DEPLOY_ENV in the terminal, and the correct value was returned. However, the script was not able to fetch it.
If you don’t care about the difference between an unset variable or a variable with an empty value, you can use the default-value parameter expansion:
If you do care about the difference, drop the colon
You can also use the -v operator to explicitly test if a parameter is set.
if [[ ! -v DEPLOY_ENV ]]; then echo "DEPLOY_ENV is not set" elif [[ -z "$DEPLOY_ENV" ]]; then echo "DEPLOY_ENV is set to the empty string" else echo "DEPLOY_ENV has the value: $DEPLOY_ENV" fi
Note that the -v option was introduced in Bash 4.2, and many systems are still running older versions of Bash.
Even bash 4.2 is over 5 years old at this point; I tend to treat anything older as a special case that deserves special mention in the question.
There is no difference between environment variables and variables in a script. Environment variables are just defined earlier, outside the script, before the script is called. From the script’s point of view, a variable is a variable.
You can check if a variable is defined:
if [ -z "$a" ] then echo "not defined" else echo "defined" fi
and then set a default value for undefined variables or do something else.
The -z checks for a zero-length (i.e. empty) string. See man bash and look for the CONDITIONAL EXPRESSIONS section.
You can also use set -u at the beginning of your script to make it fail once it encounters an undefined variable, if you want to avoid having an undefined variable breaking things in creative ways.