If you want to set a variable in your shell (as opposed to the superuser's):
- Make sure your
.bashrcexists
ls -A ~/.bashrc
- Add your variable at the bottom
export VARIABLE=value
- Save, then open a new terminal and verify the variable is set
$ echo $VARIABLE
value
Keep in mind this depends on your shell.
Answer from marcdahan on Stack OverflowIf you want to set a variable in your shell (as opposed to the superuser's):
- Make sure your
.bashrcexists
ls -A ~/.bashrc
- Add your variable at the bottom
export VARIABLE=value
- Save, then open a new terminal and verify the variable is set
$ echo $VARIABLE
value
Keep in mind this depends on your shell.
Solution: In order to export and keep environment variables persistent on linux you should export variables decelration into one of the following files: ~/.bash_profile / ~/. bash_login / ~/.profile.
When bash is invoked as an interactive/non-interactive login shell, it first reads and executes commands from the file /etc/profile (if exists) and after looks for these following files ( in that order and do the same) ~/.bash_profile, ~/. bash_login, ~/.profile.
Example: adding secret token into my user profile.
cat << End >> ~/.profile
export SECRET_TOKEN=abc123!@#
End
output:
echo $SECRET_TOKEN
abc123!@#
How to permanently set environmental variables - Unix & Linux Stack Exchange
How to set environment variables globally and permanently so they can be available in all shell processes I open and not just single one ?
Would someone explain the environment variables like I'm a crayon eating five?
Linux needs an intuitive way to set global environmental variables (AKA getting touchscreen gestures enabled in Firefox is way too difficult)
Videos
You can add it to the file .profile or your login shell profile file (located in your home directory).
To change the environmental variable "permanently" you'll need to consider at least these situations:
- Login/Non-login shell
- Interactive/Non-interactive shell
bash
- Bash as login shell will load
/etc/profile,~/.bash_profile,~/.bash_login,~/.profilein the order - Bash as non-login interactive shell will load
~/.bashrc - Bash as non-login non-interactive shell will load the configuration specified in environment variable
$BASH_ENV
$EDITOR ~/.profile
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
zsh
$EDITOR ~/.zprofile
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
fish
set -Ux LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib
set -Ux ORACLE_HOME /usr/lib/oracle/11.2/client64
ksh
$EDITOR ~/.profile
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
bourne
$EDITOR ~/.profile
#add lines at the bottom of the file:
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH ORACLE_HOME
csh or tcsh
$EDITOR ~/.login
#add lines at the bottom of the file:
setenv LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib
setenv ORACLE_HOME /usr/lib/oracle/11.2/client64
If you want to make it permanent for all users, you can edit the corresponding files under /etc/, i.e. /etc/profile for Bourne-like shells, /etc/csh.login for (t)csh, and /etc/zsh/zprofile and /etc/zsh/zshrc for zsh.
Another option is to use /etc/environment, which on Linux systems is read by the PAM module pam_env and supports only simple assignments, not shell-style expansions. (See Debian's guide on this.)
These files are likely to already contain some assignments, so follow the syntax you see already present in your file.
Make sure to restart the shell and relogin the user, to apply the changes.
If you need to add system wide environment variable, there's now /etc/profile.d folder that contains sh script to initialize variable.
You could place your sh script with all you exported variables here.
Be carefull though this should not be use as a standard way of adding variable to env on Debian.
To do if for all users/shells, depending on distro you could use /etc/environment or /etc/profile. Creating a new file in /etc/profile.d may be preferable if it exists, as it will be less likely to conflict with updates made by the packaging system.
In /etc/environment, variables are usually set with name=value, eg:
ORACLE_HOME=/usr/lib/oracle/11.2/client64
In /etc/profile, you must use export since this is a script, eg:
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
Same goes for a file under /etc/profile.d, there also may be naming restrictions which must be met for the file to work. On Debian, the file must have the extension .sh (although does not need a bang line or executable permissions since it is sourced). check your distro documentation or look at the /etc/profile script to see how these files are loaded.
Note also though that setting LD_LIBRARY_PATH permanently is potentially problematic, including being a security risk. As an alternative, I would suggest finding some way to prepend the LD_LIBRARY_PATH to the start of the command line for each program that needs it before running. Eg:
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib myprog
One way to do this is to use a wrapper script to run the program. You could give this the same name as your program and put it in /usr/local/bin or anywhere that appears before the location of your program in PATH. Here is an example script (don't forget to chmod +x the script):
#!/bin/sh
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib /real/location/of/myprog "$@"
Hello. I am still noob in Linux ecosystem. I wanted to set some environment variables and ran command export VAR=value , but as it turns out this only sets an env variable in a particular shell process and when I open other shell and run printenv I no longer see those environment variables that I set.
How can I set env variables that can be used in all shells ? Should I set it in /etc/profile.d/ directory or is there some other location I should write my env variables to ? If this is not the right directory where should I define the env variables ?
As mentioned in your own answer and the one given by @Fegnoid, exporting the variables in a .sh file inside /etc/profile.d/ or in ~/.bash_profile would do the trick. Keep in mind that if you intend to use these environment variables in a service script, it might not work as you expect since service purges all environment variables except a few.
See https://unix.stackexchange.com/a/44378/148497.
Export the variables in the ~/.profile or ~/.bash_profile of the user to have them set on login so the line would be
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib
export LD_LIBRARY_PATH
Environments for shells
Essentially, anything that runs processes will tend to read a configuration file on starting up, and to affect the environment of that, you need to hit its configuration file.
For user shells, "obvious" places are .profile, .bashrc, .bash_profile (I think) and maybe a couple of others I don't remember. Obviously, more and others if you use zsh, csh, tcsh or whatever as a shell.
There are initialization files read by your windowing environment, which may be either KDE or Gnome. The particular window manager you run underneath that may also read a config file. I admit I don't know the names of those files even for my own installation.
Finally, there are usually "master" configuration files for all those environments somewhere in /etc. They provide defaults for stuff the users don't.
I think that programs that install themselves conscientiously check the various possibilities. Various Linux distributions may offer some helper scripts for this.
cron
This one is a lot easier. For security reasons, cron only passes a couple of environment variables to subprocesses, ALWAYS. I think USER is one of those, and MAILTO another. As far as I know, there's no PATH set - this often annoys newbies. The environment of a cron job is completely different from your shell environment! Anything you want in the environment, you either pass in on the command line in crontab, or you start up a script and let that set up whatever environment it needs.
To apply a bashrc change to all users, you can modify /etc/bash.bashrc (This is for Ubuntu).
Also, as indicated in the answer above, make sure that this file is sourced by /etc/profile.