How do you write a function in bash that executes the command that it is given as an argument, where
You (uh, I) can use printf %q to escape the arguments. At first sight, escaping with printf and then doing eval always gives the same result as passing the arguments directly.
wrap() {
echo Starting: "$@"
eval $(printf "%q " "$@")
}
It seems to be possible with a double eval
:
eval "eval x=($(alias y | cut -s -d '=' -f 2))"
# now the array x contains the split expansion of alias y
"${x[@]}" "${other_args[@]}"
So maybe your function could be written as follows:
wrap() {
eval "eval prefix=($(alias $1 | cut -s -d '=' -f 2))"
shift
"${prefix[@]}" "$@"
}
However, eval
is evil, and double eval
is double evil, and aliases are not expanded in scripts for a reason.