Re:TIL: timeout in Bash scripts
by epr on Monday May 26, 2025 @12:38PM
(#44099022)
I'm generally not a huge fan of inlining the command or cluttering up my local directory with little scripts to get around the fact that it must be a subprocess you can send a signal to. I use a wrapper like this, which exports a function containing whatever complex logic I want to time out. The funky quoting in the timeout bash -c argument is a generalized version of what aidenn0 mentioned in another comment here (passing in args safely to subproc).
[ Parent
| on HN ]
You need `"$@"`, not just `$@` at the end of the command. Otherwise it will split any arguments that have spaces in them. E.g. try
long_fn() {
echo "$1"
sleep "$2"
}
to 1s long_fn "This has spaces in it" 5