For the shell to evaluate shell code stored in a variable, you'd use the eval
special builtin command. That's the same as in several other languages:
cmd="--name=cloudflare-ddns \
--hostname=oznu-cloudflare-ddns \
--env=SUBDOMAIN=private \
--env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
--env=QEMU_ARCH=x86_64 \
--env=S6_KEEP_ENV=1 \
--env=S6_BEHAVIOUR_IF_STAGE2_FAILS=2 \
--env=CF_API=https://api.cloudflare.com/client/v4 \
--env=RRTYPE=A
--env='CRON=*/5 * * * *' --env=PROXIED=false \
--env=ZONE=thebiermans.net \
--env=API_KEY=kka \
--network=host \
--restart=always \
--log-driver=db --runtime=runc --detach=true -t oznu/cloudflare-ddns:latest"
eval "docker run $cmd"
That assumes the concatenation of "docker run "
and the contents of $CMD
forms valid code in the syntax of the shell. In that case, the interpretation of that code will result in the execution of the docker
command with a list of arguments.
To run a command with a list of arguments stored in a variable, you'd use an array variable:
args=(
--name=cloudflare-ddns
--hostname=oznu-cloudflare-ddns
--env=SUBDOMAIN=private
--env=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
--env=QEMU_ARCH=x86_64
--env=S6_KEEP_ENV=1
--env=S6_BEHAVIOUR_IF_STAGE2_FAILS=2
--env=CF_API=https://api.cloudflare.com/client/v4
--env=RRTYPE=A
--env='CRON=*/5 * * * *'
--env=PROXIED=false
--env=ZONE=thebiermans.net
--env=API_KEY=kka
--network=host
--restart=always
--log-driver=db --runtime=runc --detach=true
-t oznu/cloudflare-ddns:latest
)
docker run "${args[@]}"
In any case, in bash, do not leave parameter expansions unquoted in list contexts as that hardly ever does what you want. Doing that is the split+glob operator, which splits the contents of the variable on characters of $IFS
and then performs filename generation on each resulting words. That is totally unrelated to shell syntax tokenisation and syntax parsing which are the parts that recognise and interpret quotes for instance.
echo -- "$CMD"
for example? It might give you some clues what part(s) are wrong. (One thing I see, unless it was a typo in your post here, one line does NOT have a trailing \ escape ...) – C. M. Jun 23 '21 at 10:10--env=RRTYPE=A
is missing a \ at the line end in your post, like every other line has. It should not matter here, as they are not needed anyhow due to the outer quotes, but add it if needed (or remove the others) to be consistent and certain. Anyhow, I was trying to help you see it the way the shell does to see if you could see it. Read Stephane's answer if you're still not seeing it. – C. M. Jun 23 '21 at 12:21