source /etc/environment not working from bash script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Running Ubuntu 18.04-x64
I run the following bash script:
#!/bin/bash
if [ -z $JAVA_HOME ]; then
PLACE=`which java`
QUOTE='"'
VAR_NAME="JAVA_HOME="
echo "$VAR_NAME$QUOTE$PLACE$QUOTE" >> /etc/environment
fi
source /etc/environment
I run echo $JAVA_HOME
and get a blank output
If I enter the following commands in the terminal
source /etc/environment
echo $JAVA_HOME
I get /usr/bin/java
as output
Why is source /etc/environment
not working inside the script?
command-line bash scripts environment-variables
add a comment |
Running Ubuntu 18.04-x64
I run the following bash script:
#!/bin/bash
if [ -z $JAVA_HOME ]; then
PLACE=`which java`
QUOTE='"'
VAR_NAME="JAVA_HOME="
echo "$VAR_NAME$QUOTE$PLACE$QUOTE" >> /etc/environment
fi
source /etc/environment
I run echo $JAVA_HOME
and get a blank output
If I enter the following commands in the terminal
source /etc/environment
echo $JAVA_HOME
I get /usr/bin/java
as output
Why is source /etc/environment
not working inside the script?
command-line bash scripts environment-variables
1
Are you runningecho $JAVA_HOME
from inside the script, or from the parent shell after executing the script? Thesource
command can only alter the environment of the former (unless you source the script itself)
– steeldriver
Mar 28 at 16:42
add a comment |
Running Ubuntu 18.04-x64
I run the following bash script:
#!/bin/bash
if [ -z $JAVA_HOME ]; then
PLACE=`which java`
QUOTE='"'
VAR_NAME="JAVA_HOME="
echo "$VAR_NAME$QUOTE$PLACE$QUOTE" >> /etc/environment
fi
source /etc/environment
I run echo $JAVA_HOME
and get a blank output
If I enter the following commands in the terminal
source /etc/environment
echo $JAVA_HOME
I get /usr/bin/java
as output
Why is source /etc/environment
not working inside the script?
command-line bash scripts environment-variables
Running Ubuntu 18.04-x64
I run the following bash script:
#!/bin/bash
if [ -z $JAVA_HOME ]; then
PLACE=`which java`
QUOTE='"'
VAR_NAME="JAVA_HOME="
echo "$VAR_NAME$QUOTE$PLACE$QUOTE" >> /etc/environment
fi
source /etc/environment
I run echo $JAVA_HOME
and get a blank output
If I enter the following commands in the terminal
source /etc/environment
echo $JAVA_HOME
I get /usr/bin/java
as output
Why is source /etc/environment
not working inside the script?
command-line bash scripts environment-variables
command-line bash scripts environment-variables
asked Mar 28 at 16:29
CrispJamCrispJam
53
53
1
Are you runningecho $JAVA_HOME
from inside the script, or from the parent shell after executing the script? Thesource
command can only alter the environment of the former (unless you source the script itself)
– steeldriver
Mar 28 at 16:42
add a comment |
1
Are you runningecho $JAVA_HOME
from inside the script, or from the parent shell after executing the script? Thesource
command can only alter the environment of the former (unless you source the script itself)
– steeldriver
Mar 28 at 16:42
1
1
Are you running
echo $JAVA_HOME
from inside the script, or from the parent shell after executing the script? The source
command can only alter the environment of the former (unless you source the script itself)– steeldriver
Mar 28 at 16:42
Are you running
echo $JAVA_HOME
from inside the script, or from the parent shell after executing the script? The source
command can only alter the environment of the former (unless you source the script itself)– steeldriver
Mar 28 at 16:42
add a comment |
1 Answer
1
active
oldest
votes
You could source your script instead of executing it. Then the script's source
command occurs in your current shell.
Also, JAVA_HOME should be a directory:
printf 'JAVA_HOME="%s"n' "$(dirname -- "$PLACE")" >> /etc/environment
that also allows you to drop the VAR and QUOTE vars
Also, you must use quotes: [ -z "$JAVA_HOME" ]
- if JAVA_HOME is actually empty, the shell sees
[ -z ]
- with only a single argument, the
[
command returns true if the argument itself is not empty. - Since the string "-z" is never empty, the condition is always true,
- and you'll get a new JAVA_HOME=... in the /etc/environment every time.
1
Note that/etc/environment
does not use shell syntax and the quotes might be dangerous:X="Y"
will set X to"Y"
(including the quotes).
– PerlDuck
Mar 28 at 18:56
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%2f1129457%2fsource-etc-environment-not-working-from-bash-script%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
You could source your script instead of executing it. Then the script's source
command occurs in your current shell.
Also, JAVA_HOME should be a directory:
printf 'JAVA_HOME="%s"n' "$(dirname -- "$PLACE")" >> /etc/environment
that also allows you to drop the VAR and QUOTE vars
Also, you must use quotes: [ -z "$JAVA_HOME" ]
- if JAVA_HOME is actually empty, the shell sees
[ -z ]
- with only a single argument, the
[
command returns true if the argument itself is not empty. - Since the string "-z" is never empty, the condition is always true,
- and you'll get a new JAVA_HOME=... in the /etc/environment every time.
1
Note that/etc/environment
does not use shell syntax and the quotes might be dangerous:X="Y"
will set X to"Y"
(including the quotes).
– PerlDuck
Mar 28 at 18:56
add a comment |
You could source your script instead of executing it. Then the script's source
command occurs in your current shell.
Also, JAVA_HOME should be a directory:
printf 'JAVA_HOME="%s"n' "$(dirname -- "$PLACE")" >> /etc/environment
that also allows you to drop the VAR and QUOTE vars
Also, you must use quotes: [ -z "$JAVA_HOME" ]
- if JAVA_HOME is actually empty, the shell sees
[ -z ]
- with only a single argument, the
[
command returns true if the argument itself is not empty. - Since the string "-z" is never empty, the condition is always true,
- and you'll get a new JAVA_HOME=... in the /etc/environment every time.
1
Note that/etc/environment
does not use shell syntax and the quotes might be dangerous:X="Y"
will set X to"Y"
(including the quotes).
– PerlDuck
Mar 28 at 18:56
add a comment |
You could source your script instead of executing it. Then the script's source
command occurs in your current shell.
Also, JAVA_HOME should be a directory:
printf 'JAVA_HOME="%s"n' "$(dirname -- "$PLACE")" >> /etc/environment
that also allows you to drop the VAR and QUOTE vars
Also, you must use quotes: [ -z "$JAVA_HOME" ]
- if JAVA_HOME is actually empty, the shell sees
[ -z ]
- with only a single argument, the
[
command returns true if the argument itself is not empty. - Since the string "-z" is never empty, the condition is always true,
- and you'll get a new JAVA_HOME=... in the /etc/environment every time.
You could source your script instead of executing it. Then the script's source
command occurs in your current shell.
Also, JAVA_HOME should be a directory:
printf 'JAVA_HOME="%s"n' "$(dirname -- "$PLACE")" >> /etc/environment
that also allows you to drop the VAR and QUOTE vars
Also, you must use quotes: [ -z "$JAVA_HOME" ]
- if JAVA_HOME is actually empty, the shell sees
[ -z ]
- with only a single argument, the
[
command returns true if the argument itself is not empty. - Since the string "-z" is never empty, the condition is always true,
- and you'll get a new JAVA_HOME=... in the /etc/environment every time.
answered Mar 28 at 17:31
glenn jackmanglenn jackman
12.8k2545
12.8k2545
1
Note that/etc/environment
does not use shell syntax and the quotes might be dangerous:X="Y"
will set X to"Y"
(including the quotes).
– PerlDuck
Mar 28 at 18:56
add a comment |
1
Note that/etc/environment
does not use shell syntax and the quotes might be dangerous:X="Y"
will set X to"Y"
(including the quotes).
– PerlDuck
Mar 28 at 18:56
1
1
Note that
/etc/environment
does not use shell syntax and the quotes might be dangerous: X="Y"
will set X to "Y"
(including the quotes).– PerlDuck
Mar 28 at 18:56
Note that
/etc/environment
does not use shell syntax and the quotes might be dangerous: X="Y"
will set X to "Y"
(including the quotes).– PerlDuck
Mar 28 at 18:56
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%2f1129457%2fsource-etc-environment-not-working-from-bash-script%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
1
Are you running
echo $JAVA_HOME
from inside the script, or from the parent shell after executing the script? Thesource
command can only alter the environment of the former (unless you source the script itself)– steeldriver
Mar 28 at 16:42