I would like to stringify a given command, given as an array.
This would be useful to for example put a command inside a bash -c
.
For example :
cmd=(
"printf '%s\n'"
"foo bar"
"baz bang"
)
stringified="$(transform "${cmd[@]}")"
So that : "${cmd[@]}"
and bash -c "$stringified"
would give the exact same output for any cmd array.
In my example, that would be :
$ "${cmd[@]}"
foo bar
baz bang
The value of stringified="printf \"%s\n\" \"foo bar\" \"baz bang\""
works for my case, but I wish to have a transform function that would properly escape those arguments for me.
Preferably, I would prefer a bash solution, or if that is not the case, with tools that are usually already installed on a linux distribution (printf, echo).