Questions tagged [environment-variables]

For questions pertaining to environment variables, a set of dynamic variables that can affect the running processes behavior and access to resources. Use this tag for questions about environment variables or questions about issues arising from the impact of setting or modifying the variables value in running processes behavior and access to resources.


Environment variables in Unix-like operating systems determine the behavior and access in the system. Some of the settings are contained within configuration settings, other are determined by user input. The shell keeps track of all the settings in the environment, that builds every time it starts a session with variables, defining system properties. Environment variables by convention are defined using capital letters.

The list of environmental variables is show by using commands env and printenv :

$ printenv

printenv can request for particular variable

$ printenv SHELL

env can set variables like:

$ env test_var="The test"
$ echo $TEST_VAR
The test

Shell variables are shown with the set command:

$ set

Creating shell variable:

$ SOME_VAR="test shell var"

This variable is defined only for the shell.

$ set | grep SOME_VAR

But not in the environment variables:

$ printenv | grep SOME_VAR 

Creating environment variable Shell variables can be turned into environment variables

$ export SOME_VAR

$ printenv | grep SOME_VAR

Environment variable can be turned back to shell variables

$ export -n SOME_VAR

Common environment variables

PATH – a list of directory paths. When the user types a command without providing the full path, this list is checked to see whether it contains a path that leads to the command.

HOME (Unix-like) - dicate where a user's home directory is located in the file system.

TERM – specifies the type of computer terminal or terminal emulator being used (e.g., vt100).

MAIL – used to indicate where a user's mail is to be found.

SHELL - default shell

PWD - prints current working directory

###Further reading

Is there a “.bashrc” equivalent file read by all shells?

What is the difference between 'env' and 'printenv'?

How to permanently set environmental variables

What is the difference in usage between shell variables and environment variables?

###External References

How To Read and Set Environmental and Shell Variables on a Linux VPS

env utility specification (The Open Group Base Specifications Issue 7, 2018 edition)

Environment Variables (The Open Group Base Specifications Issue 7, 2018 edition)

1426 questions
12 answers

How to correctly add a path to PATH?

I'm wondering where a new path has to be added to the PATH environment variable. I know this can be accomplished by editing .bashrc (for example), but it's not clear how to do this. This way: export PATH=~/opt/bin:$PATH or this? export…
  • 16,105
  • 10
  • 29
  • 40
6 answers

What does "LC_ALL=C" do?

What does the C value for LC_ALL do in Unix-like systems? I know that it forces the same locale for all aspects but what does C do?
  • 9,030
  • 15
  • 52
  • 73
5 answers

How do I set an environment variable on the command line and have it appear in commands?

If I run export TEST=foo echo $TEST It outputs foo. If I run TEST=foo echo $TEST It does not. How can I get this functionality without using export or a script?
  • 3,917
  • 3
  • 20
  • 19
4 answers

How to permanently set environmental variables

My variables are LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib ORACLE_HOME=/usr/lib/oracle/11.2/client64 How to save these variables permanently ?
  • 14,809
  • 8
  • 18
  • 21
9 answers

How to determine where an environment variable came from?

I have a Linux instance that I set up some time ago. When I fire it up and log in as root there are some environment variables that I set up but I can't remember or find where they came from. I've checked ~/.bash_profile, /etc/.bash_rc, and all…
  • 2,633
  • 3
  • 15
  • 5
5 answers

VISUAL vs. EDITOR – what’s the difference?

I generally set both VISUAL and EDITOR environment variables to the same thing, but what's the difference? Why would I set them differently? When developing apps, why should I choose to look at VISUAL before EDITOR or vice versa?
  • 55,442
  • 70
  • 180
  • 243
5 answers

$VAR vs ${VAR} and to quote or not to quote

I can write VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}" the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
  • 55,442
  • 70
  • 180
  • 243
6 answers

How do I set a user environment variable? (permanently, not session)

This is irritating me. I seen several suggestions (all using different files and syntax) and none of them worked. How do I set an environment variable for a specific user? I am on debian squeeze. What is the exact syntax I should put in the file to…
7 answers

How to run a program in a clean environment in bash?

I want to run a program in an empty environment (i.e. with no envariables set). How to do this in bash?
Eugene Yarmash
  • 13,945
  • 12
  • 43
  • 56
11 answers

How can I run a cron command with existing environmental variables?

How can I run a cron command with existing environmental variables? If I am at a shell prompt I can type echo $ORACLE_HOME and get a path. This is one of my environmental variables that gets set in my ~/.profile. However, it seems that ~/.profile…
  • 42,359
  • 67
  • 143
  • 166
4 answers

Is there a ".bashrc" equivalent file read by all shells?

Is ~/.bashrc the only place to specify user specific environment variables, aliases, modifications to PATH variable, etc? I ask because it seems that ~/.bashrc seems to be bash-only, but other shells exist too…
  • 23,932
  • 39
  • 95
  • 126
11 answers

Script to change current directory (cd, pwd)

I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
Sony Santos
  • 1,343
  • 2
  • 9
  • 7
6 answers

Why is setting a variable before a command legal in bash?

I've just encountered several answers such as to parsing a delimited text file... that use the construct: while IFS=, read xx yy zz;do echo $xx $yy $zz done < input_file where the IFS variable is set before the read command. I've been reading…
Mike Lippert
  • 1,265
  • 2
  • 10
  • 14
6 answers

What is the difference between 'env' and 'printenv'?

What is the difference between the two commands env and printenv? They both show the environment variables, and the output is exactly the same aside from _. Are there any historical reasons for there being two commands instead of one?
  • 1,263
  • 2
  • 8
  • 10
10 answers

Replace environment variables in a file with their actual values?

Is there an easy way to substitute/evaluate environment variables in a file? Like let's say I have a file config.xml that contains: instanceId $INSTANCE_ID
2 3
95 96