user3064538
user3064538

Reputation:

Pass all bash function's arguments to docker exec command?

I am trying to make an alias outside my container that will call a command inside the container:

CONTAINER="my-container-name"
container_echo() {
    docker exec "$CONTAINER" bash -c "echo $@"
}

I see this output:

$ container_echo "this is a test" second third
this is a test

So this doesn't quote my arguments correctly, it only passes the first argument. I've also tried docker exec "$CONTAINER" sh -c "cp \"$@\"" but I get a Syntax error: Unterminated quoted string error.

Upvotes: 1

Views: 958

Answers (1)

that other guy
that other guy

Reputation: 123550

The best way to do this would be simply:

docker exec "$CONTAINER" echo "$@"

If you really wanted to do it via bash -c, you should shell escape each word and concatenate them into one string:

docker exec "$CONTAINER" bash -c "echo ${*@Q}"

Upvotes: 4

Related Questions