The script below works perfectly with bash but not with sudo bash or sh. Pressing enter or ^C won't work. Only ^\ and ^Z work, but not with sudo bash.

printf '#!/bin/sh\nread var\n' > myscript
chmod +x myscript
sh -c "timeout -k 1 10 ./myscript"
# it does work when `bash` is used instead of `sh`

I know I can use read -t 10. (This is not available in sh and works only if I stop using timeout anyway.) But the reason I'm using timeout has nothing to do with read. I need it for (other stuff in) myscript. I don't need a workaround but an explanation for the bash/sudo bash thing and hopefully a solution for timeout and read.

  • With which implementation and version of sh, bash and timeout? On what system? – Stéphane Chazelas Mar 21 '18 at 10:09

I don't know yet why this is so, but has something to do with how different shells execute and treat signals. Apparently, it looks like this is some solution:

sh -c "timeout --foreground -k 1 5 ./myscript"

The same with sudo bash:

sudo bash -c "timeout --foreground -k 1 5 ./myscript"

man timeout has this explained so:


       when not running timeout directly from a shell prompt,

       allow COMMAND to read from the TTY and get TTY signals; in  this
       mode, children of COMMAND will not be timed out

I'm not sure what this all means, nor what the consequences are, but maybe this helps you or someone else will explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.