I need to write a user supplied command to a file with bash.
Testcase would be script.sh -- echo "Hello \$USER" and I want a file containing echo "Hello $USER" (or echo Hello\ $USER) which can be executed
I'm using heredoc to write to the file, so something like
cat > "${FILE}" <<EOF
Your command is:
${CMD}
"$@"
EOF
I tried using printf -v CMD '%q ' "$@" but that leads to echo Hello\ \$USER which is not what I want.
How do I do this?
%qformat ? because i just tried with the%sand it worked$*like soprintf "Your command is $*\n"$*or%swill lead toecho Hello $USER-> 2 arguments instead of 1. This is a problem for e.g.cat "foo with bar"->cat foo with barscript.sh -- 'echo "Hello $USER"'would be a piece of cake to deal with.