Why are environment variables different with `bash -c`Recursively rename files using find and sedWhy does rc.local require absolute paths? How can I run a script at startup that doesn't use them?Run an interactive bash subshell with initial commands without returning to the (“super”) shell immediatelyHow can I run a full OS in a Docker container, without specifying a command?How to prevent attach or exec in a docker containerdocker: Percona how to populate mysql server with initial dataWhy do we use a OS Base Image with Docker if containers have no Guest OS?Configure php-fpm to access environment variables in dockerStarting specific task containers from inside a coordinator containerwhy sendmail in container not working when running on different host machines?
Why isn't Tyrion mentioned in the in-universe book "A Song of Ice and Fire"?
What were the Ethiopians doing in Xerxes' army?
Can you still travel to America on the ESTA waiver program if you have been to Iran in transit?
Is there an idiom that means that you are in a very strong negotiation position in a negotiation?
A burglar's sunglasses, a lady's odyssey
What are nvme namespaces? How do they work?
Why would a rational buyer offer to buy with no conditions precedent?
What could a self-sustaining lunar colony slowly lose that would ultimately prove fatal?
Why does the hash of infinity have the digits of π?
How to let other coworkers know that I don't share my coworker's political views?
Shorten or merge multiple lines of `&> /dev/null &`
Is superuser the same as root?
Why did it take so long for Germany to allow electric scooters / e-rollers on the roads?
xcolor breaking ligatures
The Maltese Falcon
Co-author wants to put their current funding source in the acknowledgements section because they edited the paper
Why sampling a periodic signal doesn't yield a periodic discrete signal?
How to determine if a hyphen (-) exists inside a column
Why is 'additive' EQ more difficult to use than 'subtractive'?
Time complexity of an algorithm: Is it important to state the base of the logarithm?
What did the 'turbo' button actually do?
Testing using real data of the customer
Does French have the English "short i" vowel?
Need to read my home electrical Meter
Why are environment variables different with `bash -c`
Recursively rename files using find and sedWhy does rc.local require absolute paths? How can I run a script at startup that doesn't use them?Run an interactive bash subshell with initial commands without returning to the (“super”) shell immediatelyHow can I run a full OS in a Docker container, without specifying a command?How to prevent attach or exec in a docker containerdocker: Percona how to populate mysql server with initial dataWhy do we use a OS Base Image with Docker if containers have no Guest OS?Configure php-fpm to access environment variables in dockerStarting specific task containers from inside a coordinator containerwhy sendmail in container not working when running on different host machines?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How come the following, i.e. echoing $PATH
directly from bash -c
:
docker exec -i -t my_container bash -c "echo $PATH"
returns a different value for $PATH
than what follows, i.e. starting an interactive bash session and echoing the $PATH
?
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
To give some context to this question, I'd like to run a command in the container with docker exec
, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.
Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH
are set in a bash interactive session, but not if I run the command up straight.
bash docker
add a comment |
How come the following, i.e. echoing $PATH
directly from bash -c
:
docker exec -i -t my_container bash -c "echo $PATH"
returns a different value for $PATH
than what follows, i.e. starting an interactive bash session and echoing the $PATH
?
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
To give some context to this question, I'd like to run a command in the container with docker exec
, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.
Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH
are set in a bash interactive session, but not if I run the command up straight.
bash docker
In addition to what @chicks pointed out, usingdocker ... "echo $PATH"
will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH'
) to prevent premature evaluation of the PATH variable.
– Gordon Davisson
Nov 25 '16 at 3:30
add a comment |
How come the following, i.e. echoing $PATH
directly from bash -c
:
docker exec -i -t my_container bash -c "echo $PATH"
returns a different value for $PATH
than what follows, i.e. starting an interactive bash session and echoing the $PATH
?
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
To give some context to this question, I'd like to run a command in the container with docker exec
, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.
Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH
are set in a bash interactive session, but not if I run the command up straight.
bash docker
How come the following, i.e. echoing $PATH
directly from bash -c
:
docker exec -i -t my_container bash -c "echo $PATH"
returns a different value for $PATH
than what follows, i.e. starting an interactive bash session and echoing the $PATH
?
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
To give some context to this question, I'd like to run a command in the container with docker exec
, and this command is on the path if I start an interactive bash session, but isn't if I just run the command.
Using the full path of the executable isn't a workaround in this case, as the command relies on other environment variables that, just like PATH
are set in a bash interactive session, but not if I run the command up straight.
bash docker
bash docker
asked Nov 23 '16 at 19:14
Alessandro VernetAlessandro Vernet
222137
222137
In addition to what @chicks pointed out, usingdocker ... "echo $PATH"
will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH'
) to prevent premature evaluation of the PATH variable.
– Gordon Davisson
Nov 25 '16 at 3:30
add a comment |
In addition to what @chicks pointed out, usingdocker ... "echo $PATH"
will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH'
) to prevent premature evaluation of the PATH variable.
– Gordon Davisson
Nov 25 '16 at 3:30
In addition to what @chicks pointed out, using
docker ... "echo $PATH"
will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH'
) to prevent premature evaluation of the PATH variable.– Gordon Davisson
Nov 25 '16 at 3:30
In addition to what @chicks pointed out, using
docker ... "echo $PATH"
will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH'
) to prevent premature evaluation of the PATH variable.– Gordon Davisson
Nov 25 '16 at 3:30
add a comment |
3 Answers
3
active
oldest
votes
When -c
is specified bash
is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile
, ~/.bash_profile
, ~/.bash_login
, or ~/.profile
would definitely be skipped.
Also, as explained by the bash
man page:
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually
rshd
, or the secure shell daemonsshd
. If bash determines it is being run in this fashion, it reads and executes commands from~/.bashrc
and~/.bashrc
, if these files exist and are readable.
So if it doesn't think you're connecting across the network it might not read the .bashrc
file either which would skip everything not skipped by the previous step.
solution
To work around this issue I would create a script that sets the PATH
to something suitable and then run the command. If you want to use the existing .profile
or other files then you can just source it in your script.
2
why not run with bash -l -c "command"
– Max Ivak
Nov 22 '17 at 21:23
add a comment |
In your first example:
docker exec -i -t my_container bash -c "echo $PATH"
That will evaluate the $PATH
variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH
on the command line outside of docker and see that they are the same.
In your second example:
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
That will evaluate the $PATH
variable inside the container.
You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:
docker exec -i -t my_container bash -c "echo $PATH"
docker exec -i -t my_container bash -c 'echo $PATH'
1
Or... bash -c "printenv PATH"
or... bash -c "declare -p PATH"
which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup
– dave_thompson_085
May 10 at 3:48
add a comment |
Try the -l
option for bash
. It will run in login shell and load /etc/profile
.
docker exec -i -t my_container bash -lc "echo $PATH"
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
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%2fserverfault.com%2fquestions%2f816709%2fwhy-are-environment-variables-different-with-bash-c%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
When -c
is specified bash
is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile
, ~/.bash_profile
, ~/.bash_login
, or ~/.profile
would definitely be skipped.
Also, as explained by the bash
man page:
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually
rshd
, or the secure shell daemonsshd
. If bash determines it is being run in this fashion, it reads and executes commands from~/.bashrc
and~/.bashrc
, if these files exist and are readable.
So if it doesn't think you're connecting across the network it might not read the .bashrc
file either which would skip everything not skipped by the previous step.
solution
To work around this issue I would create a script that sets the PATH
to something suitable and then run the command. If you want to use the existing .profile
or other files then you can just source it in your script.
2
why not run with bash -l -c "command"
– Max Ivak
Nov 22 '17 at 21:23
add a comment |
When -c
is specified bash
is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile
, ~/.bash_profile
, ~/.bash_login
, or ~/.profile
would definitely be skipped.
Also, as explained by the bash
man page:
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually
rshd
, or the secure shell daemonsshd
. If bash determines it is being run in this fashion, it reads and executes commands from~/.bashrc
and~/.bashrc
, if these files exist and are readable.
So if it doesn't think you're connecting across the network it might not read the .bashrc
file either which would skip everything not skipped by the previous step.
solution
To work around this issue I would create a script that sets the PATH
to something suitable and then run the command. If you want to use the existing .profile
or other files then you can just source it in your script.
2
why not run with bash -l -c "command"
– Max Ivak
Nov 22 '17 at 21:23
add a comment |
When -c
is specified bash
is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile
, ~/.bash_profile
, ~/.bash_login
, or ~/.profile
would definitely be skipped.
Also, as explained by the bash
man page:
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually
rshd
, or the secure shell daemonsshd
. If bash determines it is being run in this fashion, it reads and executes commands from~/.bashrc
and~/.bashrc
, if these files exist and are readable.
So if it doesn't think you're connecting across the network it might not read the .bashrc
file either which would skip everything not skipped by the previous step.
solution
To work around this issue I would create a script that sets the PATH
to something suitable and then run the command. If you want to use the existing .profile
or other files then you can just source it in your script.
When -c
is specified bash
is not running as interactive or a login shell so it won't read the same startup scripts. Anything set in /etc/profile
, ~/.bash_profile
, ~/.bash_login
, or ~/.profile
would definitely be skipped.
Also, as explained by the bash
man page:
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually
rshd
, or the secure shell daemonsshd
. If bash determines it is being run in this fashion, it reads and executes commands from~/.bashrc
and~/.bashrc
, if these files exist and are readable.
So if it doesn't think you're connecting across the network it might not read the .bashrc
file either which would skip everything not skipped by the previous step.
solution
To work around this issue I would create a script that sets the PATH
to something suitable and then run the command. If you want to use the existing .profile
or other files then you can just source it in your script.
answered Nov 23 '16 at 21:43
chickschicks
3,08572033
3,08572033
2
why not run with bash -l -c "command"
– Max Ivak
Nov 22 '17 at 21:23
add a comment |
2
why not run with bash -l -c "command"
– Max Ivak
Nov 22 '17 at 21:23
2
2
why not run with bash -l -c "command"
– Max Ivak
Nov 22 '17 at 21:23
why not run with bash -l -c "command"
– Max Ivak
Nov 22 '17 at 21:23
add a comment |
In your first example:
docker exec -i -t my_container bash -c "echo $PATH"
That will evaluate the $PATH
variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH
on the command line outside of docker and see that they are the same.
In your second example:
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
That will evaluate the $PATH
variable inside the container.
You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:
docker exec -i -t my_container bash -c "echo $PATH"
docker exec -i -t my_container bash -c 'echo $PATH'
1
Or... bash -c "printenv PATH"
or... bash -c "declare -p PATH"
which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup
– dave_thompson_085
May 10 at 3:48
add a comment |
In your first example:
docker exec -i -t my_container bash -c "echo $PATH"
That will evaluate the $PATH
variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH
on the command line outside of docker and see that they are the same.
In your second example:
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
That will evaluate the $PATH
variable inside the container.
You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:
docker exec -i -t my_container bash -c "echo $PATH"
docker exec -i -t my_container bash -c 'echo $PATH'
1
Or... bash -c "printenv PATH"
or... bash -c "declare -p PATH"
which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup
– dave_thompson_085
May 10 at 3:48
add a comment |
In your first example:
docker exec -i -t my_container bash -c "echo $PATH"
That will evaluate the $PATH
variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH
on the command line outside of docker and see that they are the same.
In your second example:
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
That will evaluate the $PATH
variable inside the container.
You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:
docker exec -i -t my_container bash -c "echo $PATH"
docker exec -i -t my_container bash -c 'echo $PATH'
In your first example:
docker exec -i -t my_container bash -c "echo $PATH"
That will evaluate the $PATH
variable with your shell on your docker client, outside of the container, and then pass the expanded value as the command to run inside the container. You can compare the value of the above to running echo $PATH
on the command line outside of docker and see that they are the same.
In your second example:
docker exec -i -t my_container bash
root@21e6d898c3c2:/# echo $PATH
That will evaluate the $PATH
variable inside the container.
You can escape your first example, or single quote it to prevent the bash shell on your workstation from expanding it, so that it is evaluated inside the container. Either of the following would work:
docker exec -i -t my_container bash -c "echo $PATH"
docker exec -i -t my_container bash -c 'echo $PATH'
answered Nov 27 '17 at 21:22
BMitchBMitch
1,685714
1,685714
1
Or... bash -c "printenv PATH"
or... bash -c "declare -p PATH"
which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup
– dave_thompson_085
May 10 at 3:48
add a comment |
1
Or... bash -c "printenv PATH"
or... bash -c "declare -p PATH"
which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup
– dave_thompson_085
May 10 at 3:48
1
1
Or
... bash -c "printenv PATH"
or ... bash -c "declare -p PATH"
which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup– dave_thompson_085
May 10 at 3:48
Or
... bash -c "printenv PATH"
or ... bash -c "declare -p PATH"
which pass only the name PATH (not its value) and let the program or shell inside the container do the lookup– dave_thompson_085
May 10 at 3:48
add a comment |
Try the -l
option for bash
. It will run in login shell and load /etc/profile
.
docker exec -i -t my_container bash -lc "echo $PATH"
add a comment |
Try the -l
option for bash
. It will run in login shell and load /etc/profile
.
docker exec -i -t my_container bash -lc "echo $PATH"
add a comment |
Try the -l
option for bash
. It will run in login shell and load /etc/profile
.
docker exec -i -t my_container bash -lc "echo $PATH"
Try the -l
option for bash
. It will run in login shell and load /etc/profile
.
docker exec -i -t my_container bash -lc "echo $PATH"
edited May 10 at 3:00
chicks
3,08572033
3,08572033
answered Nov 22 '17 at 21:23
Max IvakMax Ivak
1113
1113
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- 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%2fserverfault.com%2fquestions%2f816709%2fwhy-are-environment-variables-different-with-bash-c%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
In addition to what @chicks pointed out, using
docker ... "echo $PATH"
will substitute your PATH before it starts docker, let alone the bash shell. If you want to see the PATH of the shell running under docker, use single-quotes (docker ... 'echo $PATH'
) to prevent premature evaluation of the PATH variable.– Gordon Davisson
Nov 25 '16 at 3:30