5

I'm trying to create a Docker image with an entrypoint.sh and pass arguments when I run the Docker, the issue is when I want to use arguments with double quotes.

I search in many places, also I know is more a Bash question and how the arguments are expanded by the double quotes, but maybe someone has an idea how to avoid this.

My Dockerfile:

FROM centos:7

COPY    entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

My entrypoint.sh:

#!/bin/bash

command --display=true "$@"

Running the Docker:

docker run -ti docker-image:latest --driver="kernel-4.0 kernel-4.1"

Expected behaviour:

command --display=true --driver="kernel-4.0 kernel-4.1"

Actual behaviour:

command --display=true --driver=kernel-4.0 kernel-4.1

I tried escaping the double quotes but nothing.

Halaku
  • 17
  • 5
mrbit01
  • 181
  • 2
  • 2
  • 9
  • My expectation is that `command` would see in its argument list `--driver=kernel-4.0 kernel-4.1` as a single argument including a space, but not to include the quote marks; and you’d get the same result running the same command outside Docker. Does this match what you see? – David Maze Sep 05 '19 at 21:11
  • The command need parameters with double quotes, and in your example at the end the command received 3 parameters (`--display=true`, `--driver=kernel-4.0`, `kernel-4.1`). Because bash remove the quotes. – mrbit01 Sep 06 '19 at 12:50

1 Answers1

4

Did you try to escape \ before the value? It's working with me.

#!/bin/bash
echo "all params $@"
command --display=true "$@"

and

docker run -ti yourimage --driver=\"kernel-4.0 kernel-4.1\"

As mentioned by @David, this the expected behabout of bash script.

if you run entrypoint.sh outside of Docker, it will skip quotes.

./entrypoint.sh --driver="kernel-4.0 kernel-4.1"

#output

all params --driver=kernel-4.0 kernel-4.1
--display=true --driver=kernel-4.0 kernel-4.1

How to keep quotes in Bash arguments?

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • Doesn't work escape with `\\` still removed the quotes. Also if you try with `set -x`, you can see what is going on inside the bash how is executed. (Edit: sorry for edit the answer but I tried to add Markdown code) – mrbit01 Sep 06 '19 at 12:51
  • oh sorry, it means with `\"kernel-4.0 kernel-4.1\"` – Adiii Sep 06 '19 at 12:53
  • `\"` this worked for me,https://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters – Adiii Sep 06 '19 at 12:54
  • I landed here searching why my quoting disappeared when it was forwarded to another script, so what helped me is the solution suggested in the link How to keep quotes in Bash arguments? -- https://stackoverflow.com/a/1669548/18775 – Anton Daneyko Sep 22 '20 at 20:03