To handle the general case you have to quote variables and store the command as an array:
$ USER='foo bar' DATABASE='baz ban'
$ command=(u2api --zuser="$USER" --outfmtput=json SQL create_database name="$DATABASE")
$ printf '%q ' "${command[@]}"; echo # To get a newline at the end
u2api --zuser=foo\ bar --outfmtput=json SQL create_database name=baz\ ban
To run the command array:
"${command[@]}"
echo
ing the command works in some very simple cases:
echo u2api --zuser="$USER" --outfmtput=json SQL create_database name=$DATABASE
This is the simplest solution, but only shows you the correct output if the expansions contain no special characters such as whitespace (space, tab or newline, specifically) or escape characters. For example:
$ USER='foo bar'
$ echo u2api --zuser="$USER" --outfmtput=json SQL create_database name=$DATABASE
u2api --zuser=foo bar --outfmtput=json SQL create_database name=
Note the missing quotes around the username. You'll have the same issue with any unquoted variables containing whitespace.
§ not escaped command
) instead of doingecho "escaped command"
2) an example is environment variables during interactive shell sessionsbaz=$(ls|head -n1); foo $(bar) ${baz}
, what exactly would you like to see in the "non-executing" output?