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.

Introduction

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
XDG_VTNR=7
LC_PAPER=en_GB.UTF-8
LC_ADDRESS=en_GB.UTF-8
XDG_SESSION_ID=c2
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/aav
LC_MONETARY=en_GB.UTF-8
CLUTTER_IM_MODULE=xim

printenv can request for particular variable

$ printenv SHELL
/bin/bash

env can set variables like:

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

Shell variables are shown with the set command:

$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()

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
1274
votes
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…
Paolo
  • 16,105
  • 10
  • 29
  • 40
449
votes
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?
jcubic
  • 9,030
  • 15
  • 52
  • 73
327
votes
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?
ashleysmithgpu
  • 3,917
  • 3
  • 20
  • 19
326
votes
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 ?
user3021349
  • 14,809
  • 8
  • 18
  • 21
253
votes
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…
Joel
  • 2,633
  • 3
  • 15
  • 5
247
votes
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?
xenoterracide
  • 55,442
  • 70
  • 180
  • 243
194
votes
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?
xenoterracide
  • 55,442
  • 70
  • 180
  • 243
174
votes
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…
user4069
165
votes
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
155
votes
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…
cwd
  • 42,359
  • 67
  • 143
  • 166
134
votes
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…
Stefan
  • 23,932
  • 39
  • 95
  • 126
124
votes
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
116
votes
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
110
votes
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?
WiSaGaN
  • 1,263
  • 2
  • 8
  • 10
104
votes
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
1
2 3
95 96