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)

1518 questions
360
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
  • 16,569
127
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,433
62
votes
4 answers

Who sets $USER and $USERNAME environment variables?

Also, will these variables always match currently logged-in username (they do on my Debian system)? Can I assume their availability in other Unix(-like) systems? I'm also curious why one would use whoami instead of just reading any of these…
tshepang
  • 65,642
50
votes
5 answers

What scopes can shell variables have?

I just ran into a problem that shows me I'm not clear on the scope of shell variables. I was trying to use bundle install, which is a Ruby command that uses the value of $GEM_HOME to do its work. I had set $GEM_HOME, but the command ignored that…
Nathan Long
  • 1,623
43
votes
5 answers

where is LD_LIBRARY_PATH? how do I set the LD_LIBRARY_PATH env variable?

I am trying to build a c++ program using Unix. I got the error Linking CXX executable ../../bin/ME /usr/bin/ld: cannot find -lboost_regex-mt I heard that I just need to set the location of libboost* in my LD_LIBRARY_PATH env variable and then…
csx
  • 517
18
votes
3 answers

How can I un-export a variable, without losing its value?

Let's say I exported a variable: foo=bar export foo Now, I'd like to un-export it. That's to say, if I do sh -c 'echo "$foo"' I shouldn't get bar. foo shouldn't show up in sh -c's environment at all. sh -c is merely an example, an easy way to show…
muru
  • 72,889
16
votes
1 answer

What's the difference between "export" and "setenv"?

What's the difference between export and setenv?
The Student
  • 3,529
14
votes
2 answers

Where do EDITOR, PAGER, BROWSER environment variables come from?

When I look in man bash none of these are defined. However, random posts online refer to them; where do they come from? Or are they just a convention?
user26053
9
votes
3 answers

Is it possible to find out which files are setting/adding to environment variables, and their order of precedence?

Some of my environment variables ($PATH, $MANPATH and friends) are partially set up in different source files. I find myself wishing for a command or method to quickly gather what part, in a specific environment variable, came from what file. My…
Henrik
  • 433
8
votes
3 answers

Is there a difference between setting an environment variable to the empty string and unsetting it?

Is there any practical difference between unsetting an environment variable and setting it to the empty string?
8
votes
1 answer

`env foo=bar echo $foo` prints nothing

I think I'm having a massive brainfart. I would expect env foo=bar echo $foo ...to print bar, but I get an empty line instead. Wat. The same happens if I use the bash syntax: foo=bar echo $foo
badp
  • 2,977
6
votes
2 answers

Exception of inheritance of environment variables

Quoted from Wikipedia: By default, when a process is created it inherits a duplicate environment of its parent process, except for explicit changes made by the parent when it creates the child. At API level, these changes must be done between…
Tim
  • 101,790
5
votes
1 answer

Setting user environment variable permanently, outside shell?

Is it possible to set up an envinronment variable that can be accessible from any shell, not an specific one, and that doesn't decay as soon as your session ends? I'd like to set up a NODE_ENV variable system wide, how could I achieve that?
bevacqua
  • 153
5
votes
3 answers

Is there anyway to set a readonly environment variable?

I am using the following in openssh/telnet code, which sets the user environment. setenv("TEST_ENV", "testing", 1); But this can be modified by the user, is there anyway to make it readonly env variable?
Ram
  • 170
  • 1
  • 2
  • 5
5
votes
1 answer

What is the significance of $(logname)?

sudo sh -c 'echo "$(logname) ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/$(logname)' & sudo chmod 440 /etc/sudoers.d/$(logname) I use the above one-liner to allow the current user to execute sudo without a password, on remotely connected…
paradroid
  • 1,203
1
2 3 4 5