-1

I want to compose a command in a shell script like this:

#!/bin/sh

APPLICATION="date"
PARAMETER="-d '2020-01-01 1:23'"

CMD="${APPLICATION} ${PARAMETER}"

${CMD}

The 'PARAMETER' is supposed to hold parameters that need to be quoted themself. Unfortunately it does not work like this. Escaping them via PARAMETER="-d \"2020-01-01 1:23\"" also does not work.

Michael G.
  • 133
  • 1
  • 5

1 Answers1

0

After you've build CMD up, it is just string. It contains what can be interpreted by you as a command, but the shell sees it as a bare string.

If you want the string to reinterpret it, you need to eval it:

eval "$CMD"

However, eval is often considered evil.

Enlico
  • 23,259
  • 6
  • 48
  • 102