How can I make environment variables “exported” in a shell script stick around?how to set environment...
Capacitors with same voltage, same capacitance, same temp, different diameter?
Why would an AC motor heavily shake when driven with certain frequencies?
Why is infinite intersection "towards infinity" an empty set?
How to improvise or make pot grip / pot handle
What is the delta-v required to get a mass in Earth orbit into the sun using a SINGLE transfer?
Friend is very nitpicky about side comments I don't intend to be taken too seriously
Is this ram compatible with iMac 27"?
Short story: Interstellar inspector senses "off" nature of planet hiding aggressive culture
Why does 8 bit truecolor use only 2 bits for blue?
What exactly is Apple Cider
How is the phase of 120V AC established in a North American home?
What makes an ending "happy"?
Did the Byzantines ever attempt to move their capital to Rome?
Yet another calculator problem
How invisible hand adjusts stock prices if company is listed on multiple exchanges, under multiple currencies, and one of the currencies plunges?
Is it right to use the ideas of non-winning designers in a design contest?
Are fast interviews red flags?
How strong is aircraft-grade spruce?
PWM on 5V GPIO pin
What happens when a file that is 100% paged in to the page cache gets modified by another process
What can we do about our 9-month-old putting fingers down his throat?
Automatically end list item with proper punctuation (semicolon, period)
How can faith be maintained in a world of living gods?
Infinitely many primes
How can I make environment variables “exported” in a shell script stick around?
how to set environment variables from a shell script?How can I set environment variable permanently through shell script?How to export an environment variable from .sh file in zsh?Save command output as environment variablesShell script executing in the terminal but not from shell script fileBash command does not execute from script, but works when entered in terminalScript that exports enviromental variables cannot export themSetting proxy environment variables in a shell scriptsetting bash shell varible to pwdBash environment variables not settingHow to make exported shell variables permanent?How can I set environment variable permanently through shell script?how to prevent exported environment variables to be recognized/accessible by a child shell (bash)?how to set environment variables from a shell script?at login on RHEL switch shell to bashHow can I make sudo discard all environment variables?Bash Script - Setting Local Environment Variables (Proxy)environment variables scriptPassing environment variables around in systemdimport environment variables in a bash script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME
, using a script.
I have have a shell script set up like this:
#!/bin/sh
export EC2_HOME=/home/me/.ec2
echo $EC2_HOME
When I run the script I know that EC2_HOME
is set, but I thought that using export
would make the variable stick around after the script completed. It does not, as running echo $EC_HOME
does not show anything.
I know this must be very rudimentary Linux scripting knowledge, but I don't know it. I tried looking for related questions without luck - so my apologies if this is a duplicate.
bash shell-script shell environment-variables
add a comment |
I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME
, using a script.
I have have a shell script set up like this:
#!/bin/sh
export EC2_HOME=/home/me/.ec2
echo $EC2_HOME
When I run the script I know that EC2_HOME
is set, but I thought that using export
would make the variable stick around after the script completed. It does not, as running echo $EC_HOME
does not show anything.
I know this must be very rudimentary Linux scripting knowledge, but I don't know it. I tried looking for related questions without luck - so my apologies if this is a duplicate.
bash shell-script shell environment-variables
add a comment |
I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME
, using a script.
I have have a shell script set up like this:
#!/bin/sh
export EC2_HOME=/home/me/.ec2
echo $EC2_HOME
When I run the script I know that EC2_HOME
is set, but I thought that using export
would make the variable stick around after the script completed. It does not, as running echo $EC_HOME
does not show anything.
I know this must be very rudimentary Linux scripting knowledge, but I don't know it. I tried looking for related questions without luck - so my apologies if this is a duplicate.
bash shell-script shell environment-variables
I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME
, using a script.
I have have a shell script set up like this:
#!/bin/sh
export EC2_HOME=/home/me/.ec2
echo $EC2_HOME
When I run the script I know that EC2_HOME
is set, but I thought that using export
would make the variable stick around after the script completed. It does not, as running echo $EC_HOME
does not show anything.
I know this must be very rudimentary Linux scripting knowledge, but I don't know it. I tried looking for related questions without luck - so my apologies if this is a duplicate.
bash shell-script shell environment-variables
bash shell-script shell environment-variables
edited Nov 27 '16 at 20:32
Michael Homer
55.2k9 gold badges155 silver badges188 bronze badges
55.2k9 gold badges155 silver badges188 bronze badges
asked Jan 27 '12 at 19:32
cwdcwd
15.2k56 gold badges121 silver badges159 bronze badges
15.2k56 gold badges121 silver badges159 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You should source your script, with
. ./script
or
source ./script
19
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use.
orsource
, you are not spawning a new child process, you are running the commands in the current shell.
– glenn jackman
Jan 27 '12 at 20:32
1
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do.
orsource
. Why is this happening ?
– Patryk
Feb 21 '12 at 13:03
7
@Patryk: maybe your script has anexit
statement, so it is not suitable to be sourced.
– enzotib
Feb 21 '12 at 13:24
Whilesource ./script
works completely fine,sudo source ./script.sh
sayssudo: source: command not found
. How can I do this using sudo?
– 71GA
Sep 27 '14 at 13:37
1
@71GA: depending on compilation preferences forsudo
and depending on configuration settings in/etc/sudoers
you can or cannot preserve your environment when running commands withsudo
. I suggest you to try to source your script, and then runsudo
with-E
option to preserve the environment. If it does not work, I suppose there is very little you can do.
– enzotib
Sep 27 '14 at 13:52
|
show 2 more comments
When you run a script it gets its own shell and its own environment, which disappear again as soon as the script is finished. To keep the environment variables around, source the script into your current shell:
$ source ./a.sh
or equivalently (but a little more portably) use the POSIX dot command:
$ . ./a.sh
Then the definitions will be put into your current shell's environment and be inherited by any programs you launch from it.
To be closer to running a script, . a.sh
will find a.sh by searching the directories in the PATH
environment variable.
There are some subtleties in how these behave, and whether .
and source
are the same (or present at all). . ./a.sh
will definitely behave the same in every POSIX-compatible shell, but source
and .
, and . a.sh
and . ./a.sh
, can vary. For Bash source
and .
are the same in all cases; for zsh source
always checks the current directory first; ksh is essentially similar.
If the script name is given as a path (containing a /
), that path is used directly in all cases. The most portably reliable thing to do is . ./script
or . /path/to/script
.
add a comment |
Just for record.
If you want run script from internet which exports env to system
you can use following format
source <(curl -s -L https://raw.githubusercontent.com/iamwwc/wwcdocker/master/install.sh)
For example:
source <(curl -s -L https://example.com/install.sh)
New contributor
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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/4.0/"u003ecc by-sa 4.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%2funix.stackexchange.com%2fquestions%2f30189%2fhow-can-i-make-environment-variables-exported-in-a-shell-script-stick-around%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should source your script, with
. ./script
or
source ./script
19
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use.
orsource
, you are not spawning a new child process, you are running the commands in the current shell.
– glenn jackman
Jan 27 '12 at 20:32
1
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do.
orsource
. Why is this happening ?
– Patryk
Feb 21 '12 at 13:03
7
@Patryk: maybe your script has anexit
statement, so it is not suitable to be sourced.
– enzotib
Feb 21 '12 at 13:24
Whilesource ./script
works completely fine,sudo source ./script.sh
sayssudo: source: command not found
. How can I do this using sudo?
– 71GA
Sep 27 '14 at 13:37
1
@71GA: depending on compilation preferences forsudo
and depending on configuration settings in/etc/sudoers
you can or cannot preserve your environment when running commands withsudo
. I suggest you to try to source your script, and then runsudo
with-E
option to preserve the environment. If it does not work, I suppose there is very little you can do.
– enzotib
Sep 27 '14 at 13:52
|
show 2 more comments
You should source your script, with
. ./script
or
source ./script
19
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use.
orsource
, you are not spawning a new child process, you are running the commands in the current shell.
– glenn jackman
Jan 27 '12 at 20:32
1
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do.
orsource
. Why is this happening ?
– Patryk
Feb 21 '12 at 13:03
7
@Patryk: maybe your script has anexit
statement, so it is not suitable to be sourced.
– enzotib
Feb 21 '12 at 13:24
Whilesource ./script
works completely fine,sudo source ./script.sh
sayssudo: source: command not found
. How can I do this using sudo?
– 71GA
Sep 27 '14 at 13:37
1
@71GA: depending on compilation preferences forsudo
and depending on configuration settings in/etc/sudoers
you can or cannot preserve your environment when running commands withsudo
. I suggest you to try to source your script, and then runsudo
with-E
option to preserve the environment. If it does not work, I suppose there is very little you can do.
– enzotib
Sep 27 '14 at 13:52
|
show 2 more comments
You should source your script, with
. ./script
or
source ./script
You should source your script, with
. ./script
or
source ./script
answered Jan 27 '12 at 20:12
enzotibenzotib
36.4k10 gold badges107 silver badges97 bronze badges
36.4k10 gold badges107 silver badges97 bronze badges
19
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use.
orsource
, you are not spawning a new child process, you are running the commands in the current shell.
– glenn jackman
Jan 27 '12 at 20:32
1
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do.
orsource
. Why is this happening ?
– Patryk
Feb 21 '12 at 13:03
7
@Patryk: maybe your script has anexit
statement, so it is not suitable to be sourced.
– enzotib
Feb 21 '12 at 13:24
Whilesource ./script
works completely fine,sudo source ./script.sh
sayssudo: source: command not found
. How can I do this using sudo?
– 71GA
Sep 27 '14 at 13:37
1
@71GA: depending on compilation preferences forsudo
and depending on configuration settings in/etc/sudoers
you can or cannot preserve your environment when running commands withsudo
. I suggest you to try to source your script, and then runsudo
with-E
option to preserve the environment. If it does not work, I suppose there is very little you can do.
– enzotib
Sep 27 '14 at 13:52
|
show 2 more comments
19
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use.
orsource
, you are not spawning a new child process, you are running the commands in the current shell.
– glenn jackman
Jan 27 '12 at 20:32
1
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do.
orsource
. Why is this happening ?
– Patryk
Feb 21 '12 at 13:03
7
@Patryk: maybe your script has anexit
statement, so it is not suitable to be sourced.
– enzotib
Feb 21 '12 at 13:24
Whilesource ./script
works completely fine,sudo source ./script.sh
sayssudo: source: command not found
. How can I do this using sudo?
– 71GA
Sep 27 '14 at 13:37
1
@71GA: depending on compilation preferences forsudo
and depending on configuration settings in/etc/sudoers
you can or cannot preserve your environment when running commands withsudo
. I suggest you to try to source your script, and then runsudo
with-E
option to preserve the environment. If it does not work, I suppose there is very little you can do.
– enzotib
Sep 27 '14 at 13:52
19
19
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use
.
or source
, you are not spawning a new child process, you are running the commands in the current shell.– glenn jackman
Jan 27 '12 at 20:32
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use
.
or source
, you are not spawning a new child process, you are running the commands in the current shell.– glenn jackman
Jan 27 '12 at 20:32
1
1
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do
.
or source
. Why is this happening ?– Patryk
Feb 21 '12 at 13:03
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do
.
or source
. Why is this happening ?– Patryk
Feb 21 '12 at 13:03
7
7
@Patryk: maybe your script has an
exit
statement, so it is not suitable to be sourced.– enzotib
Feb 21 '12 at 13:24
@Patryk: maybe your script has an
exit
statement, so it is not suitable to be sourced.– enzotib
Feb 21 '12 at 13:24
While
source ./script
works completely fine, sudo source ./script.sh
says sudo: source: command not found
. How can I do this using sudo?– 71GA
Sep 27 '14 at 13:37
While
source ./script
works completely fine, sudo source ./script.sh
says sudo: source: command not found
. How can I do this using sudo?– 71GA
Sep 27 '14 at 13:37
1
1
@71GA: depending on compilation preferences for
sudo
and depending on configuration settings in /etc/sudoers
you can or cannot preserve your environment when running commands with sudo
. I suggest you to try to source your script, and then run sudo
with -E
option to preserve the environment. If it does not work, I suppose there is very little you can do.– enzotib
Sep 27 '14 at 13:52
@71GA: depending on compilation preferences for
sudo
and depending on configuration settings in /etc/sudoers
you can or cannot preserve your environment when running commands with sudo
. I suggest you to try to source your script, and then run sudo
with -E
option to preserve the environment. If it does not work, I suppose there is very little you can do.– enzotib
Sep 27 '14 at 13:52
|
show 2 more comments
When you run a script it gets its own shell and its own environment, which disappear again as soon as the script is finished. To keep the environment variables around, source the script into your current shell:
$ source ./a.sh
or equivalently (but a little more portably) use the POSIX dot command:
$ . ./a.sh
Then the definitions will be put into your current shell's environment and be inherited by any programs you launch from it.
To be closer to running a script, . a.sh
will find a.sh by searching the directories in the PATH
environment variable.
There are some subtleties in how these behave, and whether .
and source
are the same (or present at all). . ./a.sh
will definitely behave the same in every POSIX-compatible shell, but source
and .
, and . a.sh
and . ./a.sh
, can vary. For Bash source
and .
are the same in all cases; for zsh source
always checks the current directory first; ksh is essentially similar.
If the script name is given as a path (containing a /
), that path is used directly in all cases. The most portably reliable thing to do is . ./script
or . /path/to/script
.
add a comment |
When you run a script it gets its own shell and its own environment, which disappear again as soon as the script is finished. To keep the environment variables around, source the script into your current shell:
$ source ./a.sh
or equivalently (but a little more portably) use the POSIX dot command:
$ . ./a.sh
Then the definitions will be put into your current shell's environment and be inherited by any programs you launch from it.
To be closer to running a script, . a.sh
will find a.sh by searching the directories in the PATH
environment variable.
There are some subtleties in how these behave, and whether .
and source
are the same (or present at all). . ./a.sh
will definitely behave the same in every POSIX-compatible shell, but source
and .
, and . a.sh
and . ./a.sh
, can vary. For Bash source
and .
are the same in all cases; for zsh source
always checks the current directory first; ksh is essentially similar.
If the script name is given as a path (containing a /
), that path is used directly in all cases. The most portably reliable thing to do is . ./script
or . /path/to/script
.
add a comment |
When you run a script it gets its own shell and its own environment, which disappear again as soon as the script is finished. To keep the environment variables around, source the script into your current shell:
$ source ./a.sh
or equivalently (but a little more portably) use the POSIX dot command:
$ . ./a.sh
Then the definitions will be put into your current shell's environment and be inherited by any programs you launch from it.
To be closer to running a script, . a.sh
will find a.sh by searching the directories in the PATH
environment variable.
There are some subtleties in how these behave, and whether .
and source
are the same (or present at all). . ./a.sh
will definitely behave the same in every POSIX-compatible shell, but source
and .
, and . a.sh
and . ./a.sh
, can vary. For Bash source
and .
are the same in all cases; for zsh source
always checks the current directory first; ksh is essentially similar.
If the script name is given as a path (containing a /
), that path is used directly in all cases. The most portably reliable thing to do is . ./script
or . /path/to/script
.
When you run a script it gets its own shell and its own environment, which disappear again as soon as the script is finished. To keep the environment variables around, source the script into your current shell:
$ source ./a.sh
or equivalently (but a little more portably) use the POSIX dot command:
$ . ./a.sh
Then the definitions will be put into your current shell's environment and be inherited by any programs you launch from it.
To be closer to running a script, . a.sh
will find a.sh by searching the directories in the PATH
environment variable.
There are some subtleties in how these behave, and whether .
and source
are the same (or present at all). . ./a.sh
will definitely behave the same in every POSIX-compatible shell, but source
and .
, and . a.sh
and . ./a.sh
, can vary. For Bash source
and .
are the same in all cases; for zsh source
always checks the current directory first; ksh is essentially similar.
If the script name is given as a path (containing a /
), that path is used directly in all cases. The most portably reliable thing to do is . ./script
or . /path/to/script
.
edited Nov 28 '16 at 0:01
answered Jun 27 '14 at 0:07
Michael HomerMichael Homer
55.2k9 gold badges155 silver badges188 bronze badges
55.2k9 gold badges155 silver badges188 bronze badges
add a comment |
add a comment |
Just for record.
If you want run script from internet which exports env to system
you can use following format
source <(curl -s -L https://raw.githubusercontent.com/iamwwc/wwcdocker/master/install.sh)
For example:
source <(curl -s -L https://example.com/install.sh)
New contributor
add a comment |
Just for record.
If you want run script from internet which exports env to system
you can use following format
source <(curl -s -L https://raw.githubusercontent.com/iamwwc/wwcdocker/master/install.sh)
For example:
source <(curl -s -L https://example.com/install.sh)
New contributor
add a comment |
Just for record.
If you want run script from internet which exports env to system
you can use following format
source <(curl -s -L https://raw.githubusercontent.com/iamwwc/wwcdocker/master/install.sh)
For example:
source <(curl -s -L https://example.com/install.sh)
New contributor
Just for record.
If you want run script from internet which exports env to system
you can use following format
source <(curl -s -L https://raw.githubusercontent.com/iamwwc/wwcdocker/master/install.sh)
For example:
source <(curl -s -L https://example.com/install.sh)
New contributor
New contributor
answered 17 mins ago
toffeetoffee
1012 bronze badges
1012 bronze badges
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f30189%2fhow-can-i-make-environment-variables-exported-in-a-shell-script-stick-around%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