End Bash Script Loop

Sometimes when bash scripting you might want the ability to cancel your script’s loop with CTRL-C. Below is an exampl simple script which loops through a file of domains separated by line break and it digs the A record (IP address) of the domain. Read further to learn how to force quit this script.

#!/bin/bash
while read p; do
    dig $p A +short
done <listofdomains.txt

If your list of domains is large your and you want to quit this script, you cant. Pressing CTRL-C will not work. You need to add trap "echo Script ended; exit;" SIGINT SIGTERM to the beginning of your script. E.g.

#!/bin/bash
trap "echo Script ended; exit;" SIGINT SIGTERM
while read p; do
    dig $p A +short
done <listofdomains.txt

Pressing CTRL-C will now print “Script ended” to your terminal and exit the script.

Kevin Kivi

http://kevinkivi.com

I am a professional software developer. My software interests include PHP, Perl, React Native, Node.js and tinkering with anything Linux related. I also have front-end knowledge.

Among other things, on my spare time I do ultra-running, swimming, software developing for fun and watch Mr. Robot.