8
1
I defined a shell function (let's call it clock
), which I want to use as a wrapper to another command, similar to the time
function, e.g. clock ls -R
.
My shell function performs some tasks and then ends with exec "$@"
.
I'd like this function to work even with shell built-ins, e. g. clock time ls -R
should output the result of the time
built-in, and not the /usr/bin/time
executable. But exec
always ends up running the command instead.
How can I make my Bash function work as a wrapper that also accepts shell built-ins as arguments?
Edit: I just learned that time
is not a Bash built-in, but a special reserved word related to pipelines. I'm still interested in a solution for built-ins even if it does not work with time
, but a more general solution would be even better.
You need to invoke a shell explicitly, using
exec bash -c \' "$@" \'
. Unless your command in the first parameter is a recognised as a shell script, then it will be interpreted as a binary to be run directly. Alternatively, and more simply, just miss out theexec
and call"@"
from your original shell. – AFH – 2015-04-21T12:15:30.670