Reputation: 1582
I would like to pass the variable $styled
to a script inside a docker container.
The docker command would look something like
docker exec container_name "/bin/bash -c cd /where/the/script/is && ./echo.sh $styled"
the echo.sh
would look like this
#!/bin/bash
echo -e $1
the variable $styled
contains escape characters, so lets say it is:
styled=$'\e[7mSOME_TEXT\e[27m'
On my local computer i can pass the variable to a local echo.sh without any issues, unfortunately it doesn't work while using dockers exec.
the error message is something like:
./echo.sh \x1b[7mFF1SOME_TEXT\x1b[27m: no such file or directory": unknown
I am pretty sure it has to do with how escaping works, single quotes, double quotes and so on, but I am simply confused by all those layers.
I also tried:
docker exec container_name "/bin/bash -c 'cd /where/the/script/is && ./echo.sh '$P'"
which results in
stat /bin/bash -c 'cd /where/the/script/is && ./echo.sh '\x1b[7mSOME_TEXT\x1b[27m': no such file or directory"
Upvotes: 0
Views: 5321
Reputation: 189628
The argument to bash -c
needs to be quoted.
docker exec container_name /bin/bash -c "cd /where/the/script/is && ./echo.sh '$styled'"
The code you exhibit doesn't do anything useful with the cd
; if your real code cares which directory it's invoked from, maybe refactor it so it doesn't. Then this is suddenly much simpler.
docker exec container_name /where/the/script/is/echo.sh "$styled"
Still you need to fix the quoting in echo.sh
, too:
#!/bin/bash
echo -e "$1"
or perhaps better yet
#!/bin/bash
echo -e "$@"
Maybe you still need docker -it
to get output back.
The single quotes in the first snippet above are brittle, and will fail if $styled
contains an unescaped literal single quote. If you genuinely need to pass in complex quoted strings to bash -c
, maybe do someting like
docker exec container_name /bin/bash -c 'cd /where/the/script/is && ./echo.sh "$1"' -- "$styled"
Passing in literal terminal escape codes is dubious, but I guess trying to fix that antipattern is a lost cause. Maybe at least I could convince you to use printf
instead of echo -e
?
Upvotes: 2