2

I have a daemonised Docker container. I can execute multiple bash commands in one go as current user in that container like this:

docker exec -it <container_ID> /bin/bash -c "pwd; cd src; pwd"

I now need to do this through a bash script. The script is simple:

#!/usr/bin/env bash
# Here I do stuff to acquire the container_ID
docker exec -it <container_ID> -user $(id -u):$(id -g) $@

And then I pass arguments to the script, like this:

./run_in_container.sh /bin/bash -c "pwd; cd src; pwd"

Which does not work as expected, because the quotes are stripped, and what docker exec gets is /bin/bash -c pwd; cd src; pwd. So I try the following:

./run_in_container.sh /bin/bash -c '"pwd; cd src; pwd"'

And I get this error message:

cd: -c: line 0: unexpected EOF while looking for matching `"'
cd: -c: line 1: syntax error: unexpected end of file

What would be the correct way of doing this?

I doubt this is very important information in this case, but I am using Gnu bash 4.3.11.

1 Answer 1

2

Use quotes around $@:

docker exec -it <container_ID> -user $(id -u):$(id -g) "$@"
Sign up to request clarification or add additional context in comments.

1 Comment

That indeed works! As soon as I store that in a variable though, like COMMAND=$@, then using "${COMMAND}" does not work any more. I know that is not the question I asked, but any idea why assigning it to a variable makes it fail?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.