Stop Loop Bash Script in Terminal – How to Terminate a Running Bash Script

bashsignalstrap:

For example,

#!/bin/bash
while :
do
    sl
done

How to terminate this bash script?

Best Answer

The program sl purposely ignores SIGINT, which is what gets sent when you press Ctrl+C. So, firstly, you'll need to tell sl not to ignore SIGINT by adding the -e argument.

If you try this, you'll notice that you can stop each individual sl, but they still repeat. You need to tell bash to exit after SIGINT as well. You can do this by putting a trap "exit" INT before the loop.

#!/bin/bash
trap "exit" INT
while :
do
    sl -e
done