I know about $*
, $@
, \"$@\"
and even ${1+\"@\"}
and what they mean.
I need to get access to the EXACT
But I need to exactly reproduce a user-entered command line to be able to programmatically re-execute it in case of failure
You don't need your exact command line for that; you can reconstruct an equivalent (even if not identical!) shell command yourself.
#!/usr/bin/env bash
printf -v cmd_q '%q ' "$@"
echo "Executed with a command identical to: $cmd_q"
...though you don't even need that, because "$@"
can be re-executed as-is, without knowing what the command that started it was:
#!/usr/bin/env bash
printf "Program is starting; received command line equivalent to: "
printf '%q ' "$@"
printf '\n'
if [[ ! $already_restarted ]]; then
echo "About to restart ourselves:
exec "$@" # restart the program
fi