I'm running Ubuntu 11.04. I use the terminal to start a bash session, and I want to add an environment variable:
$r@hajt:~$ env THEVAR=/example
But it's not working. It shows all the variables with THEVAR being the last one, but another call to env
does not show THEVAR
anymore- env | grep THEVAR
returns nothing.
Similarly, scripts with export (export THEVAR=/example
) or other variable assignments (THEVAR=/example
) don't add the environment variable.
I know I'm doing something wrong, I know it should be something simple, but I just can't find what.
UPDATE: The real meaning of my question was this one:
(Anyway I'll choose the most voted answer and leave the edited title -that wasn't what I was asking)
env
runs a program in a modified environment, then dismisses all the changes.
To set variable only for current shell:
To set it for current shell and all processes started from current shell:
To set it permanently for all future bash sessions add such line to your
.bashrc
file in your$HOME
directory.To set it permanently, and system wide (all users, all processes) add set variable in /etc/environment:
This file only accepts variable assignments like:
Do not use the
export
keyword here.You need to logout from current user and login again so environment variables changes take place.
To set an environment variable once, use the
export
command in the prompt, not in a shell script:The variable will be set for the rest of the shell session or until unset.
To set an environment variable everytime, use the
export
command in the.bashrc
file (or the appropriate initialization file for your shell).To set an environment variable from a script, use the
export
command in the script, and thensource
the script. If you execute the script it will not work.For an explanation of the difference between sourcing and executing see this answer:
To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:
sudo -H gedit /etc/environment
e.g. if you want to add
FOO=bar
, then just writeFOO=bar
in a new lineTo get the environment/var changes to persist after the script has completed, you have to use
source ./script.sh
or the shorthand notation for source, ".", like. ./script.sh
Source will execute the commands in the script as if you have typed them in... so it does change some aspects of the script, such as exiting... so if your script checks something and decides to exit if false, for instance, via calling
exit 0
, it will terminate your current terminal / shell session.I know it's pretty late, but if you want to add an environment variable for all users (e.g. JAVA usage) - you can do the following:
1) Open
/etc/bash.bashrc
using nano (you can use whatever editor, I do not prefer VIM as it's the worst when it comes to user friendliness - nothing personal).2) Append to the file:
export VAR=path export PATH=$PATH:/bin
3) (Better if you can bounce the box) - or simply open a new SSH session and confirm using `env' command.BUT IF you want each user to have a separate setting for this, you have to make a change (scripted) to
.bashrc
file under~/.bashrc
(or/home/$USER/
if you are new to Linux systems)If you are doing things via script, one easy way to set environment variable permanently is put below statement in your script,
If you need to evaluage expressions like pwd, you can use this, for example,
If you are using Ubuntu or any Unix-based system then export the variables in the
~/.bashrc
file. It's a hidden file and you can get there through the terminal or by unhiding hidden files in the file system.Then edit the file and set
export THEVAR=/example
there, save and it's done.If you are deploying JAVA aplication using TOMCAT you can set environment variables the following way:
1.sudo su and cd to /var/lib/tomcat8/bin/ (or whichever is your tomcat bin path)
touch setenv.sh(if it doesn't exist), if file present already do 'vi setenv.sh'
chmod 777 setenv.sh (make file executable)
vi setenv.sh and set following line in setenv.sh export key=value
sudo systemctl restart tomcat.service
In your java file you can use the following code to check if the variable is set