How to pass environment variables to a non-interactive shell (with example)? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionLinux environment variable substitutionNone of the dot-files is sourced when running bash via ssh, part IIWhen is /etc/bash.bashrc invoked?How to store / load exported environment variables to / from a fileWhy does a non-interactve bash login shell ignore /etc/profile when invoked by su -?Are .bashrc and .zshrc the wrong place for environment variables?Non-interactive & non-login shell environment?Does a noninteractive login shell execute `~/.profile` or a file whose name is `$BASH_ENV`?How to set environment variables to path so it can be read from GUI and command line from the same program?Where is the PATH variable set when I start a Bash shell with env -i bash?Bash environment variables in non interactive mode
I'm having difficulty getting my players to do stuff in a sandbox campaign
Passing functions in C++
Using "nakedly" instead of "with nothing on"
Antler Helmet: Can it work?
When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?
How are presidential pardons supposed to be used?
Do we know why communications with Beresheet and NASA were lost during the attempted landing of the Moon lander?
How to rotate it perfectly?
Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
Can the prologue be the backstory of your main character?
Biased dice probability question
When is phishing education going too far?
What is the electric potential inside a point charge?
Classification of bundles, Postnikov towers, obstruction theory, local coefficients
What kind of display is this?
Is there a service that would inform me whenever a new direct route is scheduled from a given airport?
Active filter with series inductor and resistor - do these exist?
What was the last x86 CPU that did not have the x87 floating-point unit built in?
Can smartphones with the same camera sensor have different image quality?
3 doors, three guards, one stone
What are the performance impacts of 'functional' Rust?
How should I respond to a player wanting to catch a sword between their hands?
What computer would be fastest for Mathematica Home Edition?
How to pass environment variables to a non-interactive shell (with example)?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionLinux environment variable substitutionNone of the dot-files is sourced when running bash via ssh, part IIWhen is /etc/bash.bashrc invoked?How to store / load exported environment variables to / from a fileWhy does a non-interactve bash login shell ignore /etc/profile when invoked by su -?Are .bashrc and .zshrc the wrong place for environment variables?Non-interactive & non-login shell environment?Does a noninteractive login shell execute `~/.profile` or a file whose name is `$BASH_ENV`?How to set environment variables to path so it can be read from GUI and command line from the same program?Where is the PATH variable set when I start a Bash shell with env -i bash?Bash environment variables in non interactive mode
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.
Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).
[1]I tried
BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEBASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEsu -l artifactory -c echo $JAVA_HOME
bash environment-variables scripting
add a comment |
In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.
Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).
[1]I tried
BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEBASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEsu -l artifactory -c echo $JAVA_HOME
bash environment-variables scripting
add a comment |
In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.
Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).
[1]I tried
BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEBASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEsu -l artifactory -c echo $JAVA_HOME
bash environment-variables scripting
In a sh script another sh script is started with su -l <user> -c /path/to/script.sh. As far as I understood script.sh will be run in a non-interactive shell. The official documentation tries to explain how to set variables in such a non-interactive shell, namely pass them in BASH_ENV[http://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html ]. This is obviously an insufficient explanation for a beginner and there's no (real) example (didn't find one in $SEARCH_ENGINE neither).
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
can mean mean a lot [1]. Can someone please give an example of an environment variable passed to a non-interactive shell.
Just to make sure: the difference between login and non-login as well as interactive and non-interactive are clear for me (https://superuser.com/questions/636219/where-should-i-export-an-environment-variable-so-that-all-combinations-of-bash-d or referenced link if anyone is interested).
[1]I tried
BASH_ENV="\$PATH=$PATH\n\$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEBASH_ENV="$PATH=$PATHn$JAVA_HOME=$JAVA_HOME" su -l artifactory -c echo $JAVA_HOMEsu -l artifactory -c echo $JAVA_HOME
bash environment-variables scripting
bash environment-variables scripting
edited 17 hours ago
Rui F Ribeiro
42.1k1483142
42.1k1483142
asked Sep 29 '14 at 1:09
Karl RichterKarl Richter
8001823
8001823
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:
ENV=/path/to/rcfile sh
Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:
export "PATH=$PATH:/some/more/paths"; sh
The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:
#!/bin/bash
...or whatever bang line.
Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:
env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file
That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.
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/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%2funix.stackexchange.com%2fquestions%2f158117%2fhow-to-pass-environment-variables-to-a-non-interactive-shell-with-example%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:
ENV=/path/to/rcfile sh
Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:
export "PATH=$PATH:/some/more/paths"; sh
The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:
#!/bin/bash
...or whatever bang line.
Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:
env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file
That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.
add a comment |
If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:
ENV=/path/to/rcfile sh
Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:
export "PATH=$PATH:/some/more/paths"; sh
The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:
#!/bin/bash
...or whatever bang line.
Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:
env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file
That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.
add a comment |
If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:
ENV=/path/to/rcfile sh
Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:
export "PATH=$PATH:/some/more/paths"; sh
The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:
#!/bin/bash
...or whatever bang line.
Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:
env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file
That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.
If it is an sh script - as in, it explicitly references #!/bin/sh - which might still be bash but would be like invoking it with --posix --no-rc --no-profile - then you can specify the ENV file with the ENV environment variable:
ENV=/path/to/rcfile sh
Specific variables need either to be declared on the command-line - as above for $ENV - or else with export. For example, for $PATH you do:
export "PATH=$PATH:/some/more/paths"; sh
The $BASH_ENV variable you reference is not a file you need to source - and it isn't interpreted anyway when bash is invoked as sh - but is rather a path to a file that is sourced when a non-interactive bash shell is invoked - such as with a script that specifies the:
#!/bin/bash
...or whatever bang line.
Another way you might like to invoke your script/shell is with the env utility. It can be used to explicitly remove values from the environment, or else, as is usually easiest, to wipe it clean from the start:
env - BASH_ENV=/path/to/rcfile /usr/bin/bash /some/script/file
That will tell env to invoke the /usr/bin/bash command - with all its arguments appended - with the $BASH_ENV environment variable specified, but otherwise with a clean environment entirely.
edited Sep 29 '14 at 1:40
answered Sep 29 '14 at 1:16
mikeservmikeserv
46.1k669164
46.1k669164
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%2f158117%2fhow-to-pass-environment-variables-to-a-non-interactive-shell-with-example%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
-bash, environment-variables, scripting