How to make ctrl+c /not/ interrupt the while-loop?

It should work if you just trap SIGINT to something. Like : (true).

#!/bin/sh
trap ":" INT    
while sleep 10s ; do
    something-that-runs-forever
done

Interrupting the something... doesn't make the shell exit now, since it ignores the signal. However, if you ^C the sleep process, it will exit with a failure, and the loop stops due to that. Move the sleep to the inside of the loop or add something like || true to prevent that.

Note that if you use trap "" INT to ignore the signal completely (instead of assigning a command to it), it's also ignored in the child process, so then you can't interrupt something... either. This is explicitly mentioned in at least Bash's manual:

If arg is the null string, then the signal specified by each sigspec is ignored by the shell and commands it invokes. [...] Signals ignored upon entry to the shell cannot be trapped or reset.

Tags:

Bash