How do I add environment variables?Why environment variable disappears after terminal reopen?Cannot set an...
Are small insurances worth it
I can't die. Who am I?
How can atoms be electrically neutral when there is a difference in the positions of the charges?
Plagiarism of code by other PhD student
What type of investment is best suited for a 1-year investment on a down payment?
What does @RC mean in SSDT SQL Server Unit Testing?
If nine coins are tossed, what is the probability that the number of heads is even?
Wrap all numerics in JSON with quotes
Can we carry rice to Japan?
What could trigger powerful quakes on icy world?
In iTunes 12 on macOS, how can I reset the skip count of a song?
Calculating Hyperbolic Sin faster than using a standard power series
I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?
What is a term for a function that when called repeatedly, has the same effect as calling once?
Borrowing Characters
What are the issues with an additional (limited) concentration slot instead of Bladesong?
Don't know what I’m looking for regarding removable HDDs?
Do higher etale homotopy groups of spectrum of a field always vanish?
Difference between 'stomach' and 'uterus'
Skis versus snow shoes - when to choose which for travelling the backcountry?
Starting index at zero
Is the withholding of funding notice allowed?
Why do phishing e-mails use faked e-mail addresses instead of the real one?
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
How do I add environment variables?
Why environment variable disappears after terminal reopen?Cannot set an environment variable using a Bash scriptIs there really a Bug in ls -l not observing the Environment Variable TIME_STYLE?How can I install Sun/Oracle's proprietary Java JDK 6/7/8 or JRE?Why should users never use normal sudo to start graphical applications?Updating golang on UbuntuAdjust brightness with xrandr and cron jobGoogle Earth and $BROWSER environment variableWhy I can't start android emulator?Setting path permanently for openmpi in ubuntuCall shell script “inline” in another shell scriptAdd custom script to PATHHow to set environment variables separated by dotsEnvironment Variable seems to be set yet not workingIs “export” only useful for temporary effects?How to use PATH env variable to read /etc/shadow file being regular userHow to set a global persistent variable?How to set environment variable through vi in Ubuntu 16.04How to export env variables as another userCan't properly reset environment variables
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: https://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell
(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.
bash
add a comment |
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: https://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell
(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.
bash
When defined/exported in .bashrc, it works properly.
– huff
Aug 27 '11 at 0:26
possible duplicate of Google Earth and $BROWSER environment variable
– Isaiah
Aug 27 '11 at 3:09
1
How aboutTHEVAR=/example
?
– Oleh Prypin
Aug 27 '11 at 6:34
2
The other is about global variables, this one does not add such a limitation. I'd keep it separate.
– Lekensteyn
Aug 27 '11 at 8:05
1
help.ubuntu.com/community/EnvironmentVariables
– huff
Aug 28 '11 at 0:08
add a comment |
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: https://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell
(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.
bash
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: https://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell
(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.
bash
bash
edited May 23 '17 at 12:39
Community♦
1
1
asked Aug 27 '11 at 0:22
huffhuff
1,5003108
1,5003108
When defined/exported in .bashrc, it works properly.
– huff
Aug 27 '11 at 0:26
possible duplicate of Google Earth and $BROWSER environment variable
– Isaiah
Aug 27 '11 at 3:09
1
How aboutTHEVAR=/example
?
– Oleh Prypin
Aug 27 '11 at 6:34
2
The other is about global variables, this one does not add such a limitation. I'd keep it separate.
– Lekensteyn
Aug 27 '11 at 8:05
1
help.ubuntu.com/community/EnvironmentVariables
– huff
Aug 28 '11 at 0:08
add a comment |
When defined/exported in .bashrc, it works properly.
– huff
Aug 27 '11 at 0:26
possible duplicate of Google Earth and $BROWSER environment variable
– Isaiah
Aug 27 '11 at 3:09
1
How aboutTHEVAR=/example
?
– Oleh Prypin
Aug 27 '11 at 6:34
2
The other is about global variables, this one does not add such a limitation. I'd keep it separate.
– Lekensteyn
Aug 27 '11 at 8:05
1
help.ubuntu.com/community/EnvironmentVariables
– huff
Aug 28 '11 at 0:08
When defined/exported in .bashrc, it works properly.
– huff
Aug 27 '11 at 0:26
When defined/exported in .bashrc, it works properly.
– huff
Aug 27 '11 at 0:26
possible duplicate of Google Earth and $BROWSER environment variable
– Isaiah
Aug 27 '11 at 3:09
possible duplicate of Google Earth and $BROWSER environment variable
– Isaiah
Aug 27 '11 at 3:09
1
1
How about
THEVAR=/example
?– Oleh Prypin
Aug 27 '11 at 6:34
How about
THEVAR=/example
?– Oleh Prypin
Aug 27 '11 at 6:34
2
2
The other is about global variables, this one does not add such a limitation. I'd keep it separate.
– Lekensteyn
Aug 27 '11 at 8:05
The other is about global variables, this one does not add such a limitation. I'd keep it separate.
– Lekensteyn
Aug 27 '11 at 8:05
1
1
help.ubuntu.com/community/EnvironmentVariables
– huff
Aug 28 '11 at 0:08
help.ubuntu.com/community/EnvironmentVariables
– huff
Aug 28 '11 at 0:08
add a comment |
6 Answers
6
active
oldest
votes
To set variable only for current shell:
VARNAME="my value"
To set it for current shell and all processes started from current shell:
export VARNAME="my value" # shorter, less portable version
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:
sudo -H gedit /etc/environment
This file only accepts variable assignments like:
VARNAME="my value"
Do not use the export
keyword here.
You need to logout from current user and login again so environment variables changes take place.
19
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
help.ubuntu.com/community/EnvironmentVariables
– SudoSURoot
Aug 22 '15 at 8:38
1
@Mr.Hyde yes, that's probably a reasonable place
– Michał Šrajer
Dec 7 '15 at 16:55
1
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.
– Michał Šrajer
Mar 18 '16 at 17:19
1
Once you set environment variable, how can I quickly view it to make sure it is set?
– Rod
Sep 27 '18 at 13:54
1
@Rodecho $myvar
– Qwerty
Oct 16 '18 at 12:35
|
show 2 more comments
To set an environment variable once, use the export
command in the prompt, not in a shell script:
$ export THEVAR=/example
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 then source
the script. If you execute the script it will not work.
For an explanation of the difference between sourcing and executing see this answer: https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-script/176788#176788
3
How can I make it work only by executing the script ? Without sourcing.
– Mithril
Nov 9 '17 at 1:53
add a comment |
To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:
- Open a terminal (by pressing CtrlAltT)
sudo -H gedit /etc/environment
- Type your password
- Edit the text file just opened:
e.g. if you want to addFOO=bar
, then just writeFOO=bar
in a new line - Save it
- Once saved, logout and login again.
- Your required changes are made.
5
I have cleaned up your answer removing the more dangerous bits aboutsudo gedit
(See askubuntu.com/questions/270006/…) andchmod 777
. The latter should never be done on a system configuration file.
– muru
Dec 8 '14 at 13:45
This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions likeif
ortest
)
– Tom Harrison Jr
Nov 9 '16 at 18:44
If bar is a string, should I put it between quotation marks? LikeFoo="bar"
?
– blue_chip
Nov 10 '16 at 17:33
@blue_chip No, works without quotes.
– Click Upvote
Jan 10 '17 at 3:20
3
@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.
– Ruslan
Feb 3 '17 at 14:40
add a comment |
To get the environment/var changes to persist after the script has completed, you have to usesource ./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.
This is a very old question that already has an accepted answer and a few others... Consider, answering more recent questions please...
– NerdOfCode
Mar 16 '18 at 18:13
@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see... There are in fact two badges available for providing good answers to old questions, so this is encouraged
– Zanna
Mar 27 '18 at 20:08
add a comment |
If you are doing things via script, one easy way to set environment variable permanently is put below statement in your script,
if [[ ! -d "$MyVar" ]]; then
export MyVar="abc"
echo 'export MyVar="abc"' >> ~/.bashrc
fi
If you need to evaluage expressions like pwd, you can use this, for example,
echo "export EIGEN_ROOT="$(pwd)/eigen"" >> ~/.bashrc
add a comment |
I tried adding environment variables for oracle using sudo -H gedit /etc/environment to set the values permanently.
but after restarting also the changes are not reflected. what could be the reason any idea?
when i check env in terminal it is not showing up. but when i check with sudo -H gedit /etc/environment, it shows up. i dont know why it is not updated. please help me
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f58814%2fhow-do-i-add-environment-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
To set variable only for current shell:
VARNAME="my value"
To set it for current shell and all processes started from current shell:
export VARNAME="my value" # shorter, less portable version
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:
sudo -H gedit /etc/environment
This file only accepts variable assignments like:
VARNAME="my value"
Do not use the export
keyword here.
You need to logout from current user and login again so environment variables changes take place.
19
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
help.ubuntu.com/community/EnvironmentVariables
– SudoSURoot
Aug 22 '15 at 8:38
1
@Mr.Hyde yes, that's probably a reasonable place
– Michał Šrajer
Dec 7 '15 at 16:55
1
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.
– Michał Šrajer
Mar 18 '16 at 17:19
1
Once you set environment variable, how can I quickly view it to make sure it is set?
– Rod
Sep 27 '18 at 13:54
1
@Rodecho $myvar
– Qwerty
Oct 16 '18 at 12:35
|
show 2 more comments
To set variable only for current shell:
VARNAME="my value"
To set it for current shell and all processes started from current shell:
export VARNAME="my value" # shorter, less portable version
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:
sudo -H gedit /etc/environment
This file only accepts variable assignments like:
VARNAME="my value"
Do not use the export
keyword here.
You need to logout from current user and login again so environment variables changes take place.
19
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
help.ubuntu.com/community/EnvironmentVariables
– SudoSURoot
Aug 22 '15 at 8:38
1
@Mr.Hyde yes, that's probably a reasonable place
– Michał Šrajer
Dec 7 '15 at 16:55
1
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.
– Michał Šrajer
Mar 18 '16 at 17:19
1
Once you set environment variable, how can I quickly view it to make sure it is set?
– Rod
Sep 27 '18 at 13:54
1
@Rodecho $myvar
– Qwerty
Oct 16 '18 at 12:35
|
show 2 more comments
To set variable only for current shell:
VARNAME="my value"
To set it for current shell and all processes started from current shell:
export VARNAME="my value" # shorter, less portable version
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:
sudo -H gedit /etc/environment
This file only accepts variable assignments like:
VARNAME="my value"
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 variable only for current shell:
VARNAME="my value"
To set it for current shell and all processes started from current shell:
export VARNAME="my value" # shorter, less portable version
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:
sudo -H gedit /etc/environment
This file only accepts variable assignments like:
VARNAME="my value"
Do not use the export
keyword here.
You need to logout from current user and login again so environment variables changes take place.
edited Nov 20 '17 at 8:43
muru
1
1
answered Aug 27 '11 at 7:50
Michał ŠrajerMichał Šrajer
17.4k42528
17.4k42528
19
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
help.ubuntu.com/community/EnvironmentVariables
– SudoSURoot
Aug 22 '15 at 8:38
1
@Mr.Hyde yes, that's probably a reasonable place
– Michał Šrajer
Dec 7 '15 at 16:55
1
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.
– Michał Šrajer
Mar 18 '16 at 17:19
1
Once you set environment variable, how can I quickly view it to make sure it is set?
– Rod
Sep 27 '18 at 13:54
1
@Rodecho $myvar
– Qwerty
Oct 16 '18 at 12:35
|
show 2 more comments
19
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
help.ubuntu.com/community/EnvironmentVariables
– SudoSURoot
Aug 22 '15 at 8:38
1
@Mr.Hyde yes, that's probably a reasonable place
– Michał Šrajer
Dec 7 '15 at 16:55
1
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.
– Michał Šrajer
Mar 18 '16 at 17:19
1
Once you set environment variable, how can I quickly view it to make sure it is set?
– Rod
Sep 27 '18 at 13:54
1
@Rodecho $myvar
– Qwerty
Oct 16 '18 at 12:35
19
19
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
help.ubuntu.com/community/EnvironmentVariables– SudoSURoot
Aug 22 '15 at 8:38
Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.
help.ubuntu.com/community/EnvironmentVariables– SudoSURoot
Aug 22 '15 at 8:38
1
1
@Mr.Hyde yes, that's probably a reasonable place
– Michał Šrajer
Dec 7 '15 at 16:55
@Mr.Hyde yes, that's probably a reasonable place
– Michał Šrajer
Dec 7 '15 at 16:55
1
1
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.
– Michał Šrajer
Mar 18 '16 at 17:19
@BharadwajRaju because on some old UNIX systems one can export only variable that is set already. Old Solaris and HP-UX for example.
– Michał Šrajer
Mar 18 '16 at 17:19
1
1
Once you set environment variable, how can I quickly view it to make sure it is set?
– Rod
Sep 27 '18 at 13:54
Once you set environment variable, how can I quickly view it to make sure it is set?
– Rod
Sep 27 '18 at 13:54
1
1
@Rod
echo $myvar
– Qwerty
Oct 16 '18 at 12:35
@Rod
echo $myvar
– Qwerty
Oct 16 '18 at 12:35
|
show 2 more comments
To set an environment variable once, use the export
command in the prompt, not in a shell script:
$ export THEVAR=/example
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 then source
the script. If you execute the script it will not work.
For an explanation of the difference between sourcing and executing see this answer: https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-script/176788#176788
3
How can I make it work only by executing the script ? Without sourcing.
– Mithril
Nov 9 '17 at 1:53
add a comment |
To set an environment variable once, use the export
command in the prompt, not in a shell script:
$ export THEVAR=/example
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 then source
the script. If you execute the script it will not work.
For an explanation of the difference between sourcing and executing see this answer: https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-script/176788#176788
3
How can I make it work only by executing the script ? Without sourcing.
– Mithril
Nov 9 '17 at 1:53
add a comment |
To set an environment variable once, use the export
command in the prompt, not in a shell script:
$ export THEVAR=/example
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 then source
the script. If you execute the script it will not work.
For an explanation of the difference between sourcing and executing see this answer: https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-script/176788#176788
To set an environment variable once, use the export
command in the prompt, not in a shell script:
$ export THEVAR=/example
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 then source
the script. If you execute the script it will not work.
For an explanation of the difference between sourcing and executing see this answer: https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-script/176788#176788
edited Mar 20 '17 at 10:18
Community♦
1
1
answered Aug 27 '11 at 7:20
lesmanalesmana
12k84148
12k84148
3
How can I make it work only by executing the script ? Without sourcing.
– Mithril
Nov 9 '17 at 1:53
add a comment |
3
How can I make it work only by executing the script ? Without sourcing.
– Mithril
Nov 9 '17 at 1:53
3
3
How can I make it work only by executing the script ? Without sourcing.
– Mithril
Nov 9 '17 at 1:53
How can I make it work only by executing the script ? Without sourcing.
– Mithril
Nov 9 '17 at 1:53
add a comment |
To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:
- Open a terminal (by pressing CtrlAltT)
sudo -H gedit /etc/environment
- Type your password
- Edit the text file just opened:
e.g. if you want to addFOO=bar
, then just writeFOO=bar
in a new line - Save it
- Once saved, logout and login again.
- Your required changes are made.
5
I have cleaned up your answer removing the more dangerous bits aboutsudo gedit
(See askubuntu.com/questions/270006/…) andchmod 777
. The latter should never be done on a system configuration file.
– muru
Dec 8 '14 at 13:45
This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions likeif
ortest
)
– Tom Harrison Jr
Nov 9 '16 at 18:44
If bar is a string, should I put it between quotation marks? LikeFoo="bar"
?
– blue_chip
Nov 10 '16 at 17:33
@blue_chip No, works without quotes.
– Click Upvote
Jan 10 '17 at 3:20
3
@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.
– Ruslan
Feb 3 '17 at 14:40
add a comment |
To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:
- Open a terminal (by pressing CtrlAltT)
sudo -H gedit /etc/environment
- Type your password
- Edit the text file just opened:
e.g. if you want to addFOO=bar
, then just writeFOO=bar
in a new line - Save it
- Once saved, logout and login again.
- Your required changes are made.
5
I have cleaned up your answer removing the more dangerous bits aboutsudo gedit
(See askubuntu.com/questions/270006/…) andchmod 777
. The latter should never be done on a system configuration file.
– muru
Dec 8 '14 at 13:45
This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions likeif
ortest
)
– Tom Harrison Jr
Nov 9 '16 at 18:44
If bar is a string, should I put it between quotation marks? LikeFoo="bar"
?
– blue_chip
Nov 10 '16 at 17:33
@blue_chip No, works without quotes.
– Click Upvote
Jan 10 '17 at 3:20
3
@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.
– Ruslan
Feb 3 '17 at 14:40
add a comment |
To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:
- Open a terminal (by pressing CtrlAltT)
sudo -H gedit /etc/environment
- Type your password
- Edit the text file just opened:
e.g. if you want to addFOO=bar
, then just writeFOO=bar
in a new line - Save it
- Once saved, logout and login again.
- Your required changes are made.
To permanently add a new environment variable in Ubuntu (tested only in 14.04), use the following steps:
- Open a terminal (by pressing CtrlAltT)
sudo -H gedit /etc/environment
- Type your password
- Edit the text file just opened:
e.g. if you want to addFOO=bar
, then just writeFOO=bar
in a new line - Save it
- Once saved, logout and login again.
- Your required changes are made.
edited Dec 8 '14 at 13:38
muru
1
1
answered Dec 7 '14 at 8:45
user355501user355501
19112
19112
5
I have cleaned up your answer removing the more dangerous bits aboutsudo gedit
(See askubuntu.com/questions/270006/…) andchmod 777
. The latter should never be done on a system configuration file.
– muru
Dec 8 '14 at 13:45
This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions likeif
ortest
)
– Tom Harrison Jr
Nov 9 '16 at 18:44
If bar is a string, should I put it between quotation marks? LikeFoo="bar"
?
– blue_chip
Nov 10 '16 at 17:33
@blue_chip No, works without quotes.
– Click Upvote
Jan 10 '17 at 3:20
3
@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.
– Ruslan
Feb 3 '17 at 14:40
add a comment |
5
I have cleaned up your answer removing the more dangerous bits aboutsudo gedit
(See askubuntu.com/questions/270006/…) andchmod 777
. The latter should never be done on a system configuration file.
– muru
Dec 8 '14 at 13:45
This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions likeif
ortest
)
– Tom Harrison Jr
Nov 9 '16 at 18:44
If bar is a string, should I put it between quotation marks? LikeFoo="bar"
?
– blue_chip
Nov 10 '16 at 17:33
@blue_chip No, works without quotes.
– Click Upvote
Jan 10 '17 at 3:20
3
@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.
– Ruslan
Feb 3 '17 at 14:40
5
5
I have cleaned up your answer removing the more dangerous bits about
sudo gedit
(See askubuntu.com/questions/270006/…) and chmod 777
. The latter should never be done on a system configuration file.– muru
Dec 8 '14 at 13:45
I have cleaned up your answer removing the more dangerous bits about
sudo gedit
(See askubuntu.com/questions/270006/…) and chmod 777
. The latter should never be done on a system configuration file.– muru
Dec 8 '14 at 13:45
This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions like
if
or test
)– Tom Harrison Jr
Nov 9 '16 at 18:44
This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions like
if
or test
)– Tom Harrison Jr
Nov 9 '16 at 18:44
If bar is a string, should I put it between quotation marks? Like
Foo="bar"
?– blue_chip
Nov 10 '16 at 17:33
If bar is a string, should I put it between quotation marks? Like
Foo="bar"
?– blue_chip
Nov 10 '16 at 17:33
@blue_chip No, works without quotes.
– Click Upvote
Jan 10 '17 at 3:20
@blue_chip No, works without quotes.
– Click Upvote
Jan 10 '17 at 3:20
3
3
@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.
– Ruslan
Feb 3 '17 at 14:40
@blue_chip for environment variables, everything is a string. You only need quotes when it contains spaces.
– Ruslan
Feb 3 '17 at 14:40
add a comment |
To get the environment/var changes to persist after the script has completed, you have to usesource ./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.
This is a very old question that already has an accepted answer and a few others... Consider, answering more recent questions please...
– NerdOfCode
Mar 16 '18 at 18:13
@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see... There are in fact two badges available for providing good answers to old questions, so this is encouraged
– Zanna
Mar 27 '18 at 20:08
add a comment |
To get the environment/var changes to persist after the script has completed, you have to usesource ./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.
This is a very old question that already has an accepted answer and a few others... Consider, answering more recent questions please...
– NerdOfCode
Mar 16 '18 at 18:13
@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see... There are in fact two badges available for providing good answers to old questions, so this is encouraged
– Zanna
Mar 27 '18 at 20:08
add a comment |
To get the environment/var changes to persist after the script has completed, you have to usesource ./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.
To get the environment/var changes to persist after the script has completed, you have to usesource ./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.
answered Mar 16 '18 at 15:09
m0blm0bl
411
411
This is a very old question that already has an accepted answer and a few others... Consider, answering more recent questions please...
– NerdOfCode
Mar 16 '18 at 18:13
@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see... There are in fact two badges available for providing good answers to old questions, so this is encouraged
– Zanna
Mar 27 '18 at 20:08
add a comment |
This is a very old question that already has an accepted answer and a few others... Consider, answering more recent questions please...
– NerdOfCode
Mar 16 '18 at 18:13
@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see... There are in fact two badges available for providing good answers to old questions, so this is encouraged
– Zanna
Mar 27 '18 at 20:08
This is a very old question that already has an accepted answer and a few others... Consider, answering more recent questions please...
– NerdOfCode
Mar 16 '18 at 18:13
This is a very old question that already has an accepted answer and a few others... Consider, answering more recent questions please...
– NerdOfCode
Mar 16 '18 at 18:13
@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see... There are in fact two badges available for providing good answers to old questions, so this is encouraged
– Zanna
Mar 27 '18 at 20:08
@NerdOfCode this answer addresses a problem that is still relevant and none of the other answers, including the accepted one, mention it, as far as I can see... There are in fact two badges available for providing good answers to old questions, so this is encouraged
– Zanna
Mar 27 '18 at 20:08
add a comment |
If you are doing things via script, one easy way to set environment variable permanently is put below statement in your script,
if [[ ! -d "$MyVar" ]]; then
export MyVar="abc"
echo 'export MyVar="abc"' >> ~/.bashrc
fi
If you need to evaluage expressions like pwd, you can use this, for example,
echo "export EIGEN_ROOT="$(pwd)/eigen"" >> ~/.bashrc
add a comment |
If you are doing things via script, one easy way to set environment variable permanently is put below statement in your script,
if [[ ! -d "$MyVar" ]]; then
export MyVar="abc"
echo 'export MyVar="abc"' >> ~/.bashrc
fi
If you need to evaluage expressions like pwd, you can use this, for example,
echo "export EIGEN_ROOT="$(pwd)/eigen"" >> ~/.bashrc
add a comment |
If you are doing things via script, one easy way to set environment variable permanently is put below statement in your script,
if [[ ! -d "$MyVar" ]]; then
export MyVar="abc"
echo 'export MyVar="abc"' >> ~/.bashrc
fi
If you need to evaluage expressions like pwd, you can use this, for example,
echo "export EIGEN_ROOT="$(pwd)/eigen"" >> ~/.bashrc
If you are doing things via script, one easy way to set environment variable permanently is put below statement in your script,
if [[ ! -d "$MyVar" ]]; then
export MyVar="abc"
echo 'export MyVar="abc"' >> ~/.bashrc
fi
If you need to evaluage expressions like pwd, you can use this, for example,
echo "export EIGEN_ROOT="$(pwd)/eigen"" >> ~/.bashrc
answered Jul 12 '17 at 2:06
Shital ShahShital Shah
24124
24124
add a comment |
add a comment |
I tried adding environment variables for oracle using sudo -H gedit /etc/environment to set the values permanently.
but after restarting also the changes are not reflected. what could be the reason any idea?
when i check env in terminal it is not showing up. but when i check with sudo -H gedit /etc/environment, it shows up. i dont know why it is not updated. please help me
New contributor
add a comment |
I tried adding environment variables for oracle using sudo -H gedit /etc/environment to set the values permanently.
but after restarting also the changes are not reflected. what could be the reason any idea?
when i check env in terminal it is not showing up. but when i check with sudo -H gedit /etc/environment, it shows up. i dont know why it is not updated. please help me
New contributor
add a comment |
I tried adding environment variables for oracle using sudo -H gedit /etc/environment to set the values permanently.
but after restarting also the changes are not reflected. what could be the reason any idea?
when i check env in terminal it is not showing up. but when i check with sudo -H gedit /etc/environment, it shows up. i dont know why it is not updated. please help me
New contributor
I tried adding environment variables for oracle using sudo -H gedit /etc/environment to set the values permanently.
but after restarting also the changes are not reflected. what could be the reason any idea?
when i check env in terminal it is not showing up. but when i check with sudo -H gedit /etc/environment, it shows up. i dont know why it is not updated. please help me
New contributor
New contributor
answered 8 mins ago
MohanaMohana
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f58814%2fhow-do-i-add-environment-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
When defined/exported in .bashrc, it works properly.
– huff
Aug 27 '11 at 0:26
possible duplicate of Google Earth and $BROWSER environment variable
– Isaiah
Aug 27 '11 at 3:09
1
How about
THEVAR=/example
?– Oleh Prypin
Aug 27 '11 at 6:34
2
The other is about global variables, this one does not add such a limitation. I'd keep it separate.
– Lekensteyn
Aug 27 '11 at 8:05
1
help.ubuntu.com/community/EnvironmentVariables
– huff
Aug 28 '11 at 0:08