I'd like to create a bash function that can get a bash code block like time does:
time {
echo 123
echo "Something else"
sleep 1
}
Basically I'd like to be able to wrap the block with my bash code.
Edit: Updated my question after the first answer of @DavidPostill♦ and the comments:
For example, I'd like to wrap a code block with 2>&1 > /dev/null and also the time it with time. Should I write a program outside bash to do that?
function myfunction() {
time { $1 } 2>&1 /dev/null
}
myfunction { sleep 1 }
Edit 2: Upon further reading it seems like it isn't possible as time is a special case for bash.
timecan do this because its a keyword in Bash. It's recognized by the parser. If you use/usr/bin/timeinstead (orcommand timeor some function or a builtin), then it will be a syntax error. What exactly do you want your function to do? Maybe we can do this using a different syntax. – Kamil Maciorowski Oct 04 '20 at 12:56time. Should I write a program outside bash to do that? – funerr Oct 04 '20 at 18:24time(and similar keywords) in call from another script. – Kamil Maciorowski Oct 04 '20 at 18:31