0

In my script I changed the shebang #!/bin/bash to #!/bin/sh. And my script has the following lines:

ctrlc(){
    echo; kill -INT 0; exit 0
}
trap ctrlc 2

In bash when I pressed ^C it was all ok, the program exits normally. But in sh the program still exits, but I got this message: /mnt/c/Users/user/Documents: 1: Maximum function recursion depth (1000) reached Why am I getting this and is there a way to not to seeing it? Thank you.

7
  • 2
    put a trap - INT; in your ctrlc function before the kill command.
    – user313992
    Dec 15, 2019 at 19:52
  • Thanks it worked! But how it worked, different from bash?
    – user386273
    Dec 15, 2019 at 19:54
  • Also I got the same problem with ^D. In bash, I solved it with adding the code || kill -INT 0 after every read. For example; read -p "[y/n] " answer || kill -INT 0But I don't know how to do it in sh?
    – user386273
    Dec 15, 2019 at 19:57
  • trap - INT has reset the signal handler for SIGINT from your handler to the default (ie terminate), preventing your script from re-entering the trap in an infinite loop. I don't think that the bash's behaviour is standard.
    – user313992
    Dec 15, 2019 at 19:59
  • 3
    Using that kill -INT 0 so liberally doesn't sound like a good idea -- if your script is called from another script (or non-interactive daemon, etc) both will run in the same process group and kill -INT 0 will kill both.
    – user313992
    Dec 15, 2019 at 20:01

1 Answer 1

-2

You could try this

 trap 'echo " "; echo "PROGRAM INTERRUPTED"; echo " "; exit 1' INT
1
  • They could, but could you explain why they should?
    – Kusalananda
    Dec 18, 2019 at 7:33

You must log in to answer this question.