0

I am trying to write a generic script for restarting a Docker container. I set the container name, repo and run options in variables at the start. The run options are a multiline variable to make it easier to read/edit i.e.

runOptions='
-e PUID=1001 -e PGID=1001
-e TZ=Europe/London
--health-cmd "curl -sf https://example.com || exit 1"
--health-interval 1m
--health-timeout 10s
--health-retries 1'

If I echo the final command it looks fine BUT when I execute the command in the script it fails.

${containerRuntime} run -d --name ${containerName} ${runOptions} ${containerRepo}

gives:

Error: unknown shorthand flag: 's' in -sf
See 'podman run --help'

I assume the issue is with the double quotes not being interpreted correctly when run via the script. I tried escaping the double quotes but the behaviour is the same.

Any ideas how I can get it to work?

Thank you

UNiXMIT
  • 3
  • 2
  • 1
    [I'm trying to put a command in a variable, but the complex cases always fail! — BashFAQ/050](https://mywiki.wooledge.org/BashFAQ/050) – Biffen Aug 21 '23 at 08:55
  • heredoc should work here -- ` $ runOptions=$(cat << EOF -e PUID=1001 -e PGID=1001 -e TZ=Europe/London --health-cmd "curl -sf https://example.com || exit 1" --health-interval 1m --health-timeout 10s --health-retries 1' EOF )` – M S Aug 21 '23 at 09:08
  • Your $runOptions is split at the spaces into individual arguments. The newlines are your least problem - the whole approach does not work. You want that `"curl -sf https://example.com || exit 1"` is passed as a single argument, but because of the spaces, 6 separate arguments are passed. `-sf` is the first one which causes trouble and produces the error message, but a lone `||` also does not make sense as argument. – user1934428 Aug 21 '23 at 09:13
  • 1
    Though the linked duplicate might be related to `curl`, but the idea is the same. Define an array with the list of options and pass it as a quoted-expansion – Inian Aug 21 '23 at 09:24

0 Answers0