1
u2api --zuser="$USER" --outfmtput=json SQL create_database name=$DATABASE

I would like to easily (e.g. no escaping if possible) print the resulting command instead of executing it.

Is there any builtin facility to do this kind of testing in default linux shells like bash ?

3
  • What do you mean by "no escaping"? What would you like to see that is different compared to just looking at the source code? Feb 5, 2020 at 12:46
  • 1)e.g. prepending some special character to the command (e.g. § not escaped command) instead of doing echo "escaped command" 2) an example is environment variables during interactive shell sessions
    – beppe9000
    Feb 5, 2020 at 20:42
  • I still don't understand. For instance, if your commands are baz=$(ls|head -n1); foo $(bar) ${baz}, what exactly would you like to see in the "non-executing" output? Feb 7, 2020 at 7:48

1 Answer 1

1

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[@]}"

echoing 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .