1

I have exported variables in /etc/profile.d/custom.sh and ~/.bashrc. They are available in the environment when I run docker exec -i CONT_ID /bin/bash. But when I run another command, they are not available in command's environment. It's right, so I try to do this:

docker exec -i "$MYCONTNAME" /bin/bash -c "source /etc/profile.d/custom.sh; echo $MYVAR"

It does not work. I try even:

docker exec -i "$MYCONTNAME" /bin/bash -c "export Y=123; echo Y=$Y"
docker exec -i "$MYCONTNAME" /bin/bash -c "export Y=123 && echo Y=$Y"

but they don't work either. I can add -e VARi=VALi but they are located in shell script file with as exporting variables, so is it any way to get working way with /bin/bash (source /etc/profile.d/custom.sh) ?

3

$MYVAR in the command

docker exec -i "$MYCONTNAME" /bin/bash -c "source /etc/profile.d/custom.sh; echo $MYVAR"

will be expanded before /bin/bash -c is executed (since it's in double quotes).

Instead, use single quotes:

docker exec -i "$MYCONTNAME" /bin/bash -c 'source /etc/profile.d/custom.sh; echo $MYVAR'

Alternatively (untested),

docker exec -i "$MYCONTNAME" /usr/bin/env BASH_ENV=/etc/profile.d/custom.sh /bin/bash -c 'echo $MYVAR'

If BASH_ENV is set when a non-interactive shell is started (with e.g. bash -c), the file that the variable points to will be sourced before the shell runs its commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.