Subhamoy S.
Subhamoy S.

Reputation: 6764

Bash Command as Bash Command Argument

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.

Upvotes: 2

Views: 69

Answers (1)

txtechhelp
txtechhelp

Reputation: 6787

Use quotes around $@:

docker exec -it <container_ID> -user $(id -u):$(id -g) "$@"

Upvotes: 2

Related Questions