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.
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.