Use the following code :
#!/bin/bash
# type "finish" to exit
stty -echoctl # hide ^C
# function called by trap
other_commands() {
tput setaf 1
printf "\rSIGINT caught "
tput sgr0
sleep 1
printf "\rType a command >>> "
}
trap 'other_commands' SIGINT
input="$@"
while true; do
printf "\rType a command >>> "
read input
[[ $input == finish ]] && break
bash -c "$input"
done
Answer from Gilles Quénot on Stack OverflowUse the following code :
#!/bin/bash
# type "finish" to exit
stty -echoctl # hide ^C
# function called by trap
other_commands() {
tput setaf 1
printf "\rSIGINT caught "
tput sgr0
sleep 1
printf "\rType a command >>> "
}
trap 'other_commands' SIGINT
input="$@"
while true; do
printf "\rType a command >>> "
read input
[[ $input == finish ]] && break
bash -c "$input"
done
For bash :
#!/bin/bash
trap ctrl_c INT
function ctrl_c() {
echo "Ctrl + C happened"
}
For sh:
#!/bin/sh
trap ctrl_c INT
ctrl_c () {
echo "Ctrl + C happened"
}
How to handle ctrl+c in bash scripts
Trap Ctrl C in script - Unix & Linux Stack Exchange
signals - Termination of ssh with Ctrl-C trap in bash script - Unix & Linux Stack Exchange
Trap 'Ctrl + c' for bash script but not for process open in this script - Unix & Linux Stack Exchange
Videos
Hello Guys!
I have wrote an article on Medium on how to handle ctrl+c in bash scripts using the 'trap' command
For Medium users with a subscription: https://lovethepenguin.com/how-to-handle-ctrl-c-in-bash-scripts-d7085e7d3d47
For Medium users without a subscription: https://lovethepenguin.com/how-to-handle-ctrl-c-in-bash-scripts-d7085e7d3d47?sk=8a9020256b1498196a923c5521619228
Please comment on what you liked, did you find this article useful?
Add this somewhere near the top of your script:
trap cleanup SIGINT
cleanup () {
sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
}
I'm not sure why you are sending marlin across the pipe to systemctl when you reenable but the trap command you have commented out would probably still work except you have it using mask instead of unmask fwiw. Although it's worth noting that by trapping for INT and EXIT when you press ctrl+c your command will be executed twice.
I have experimented with the answer from jesse_b above, but it didn't work for me because the process before the Ctrl+C is kinda a blocking process, so what I would do is to put the function before the trap call.
#!/bin/bash
trp()
{
sudo airmon-ng stop wlp2s0mon
service NetworkManager start
}
trap trp SIGINT
sudo airmon-ng check kill
sudo airmon-ng start wlp2s0
sudo aireplay-ng --deauth 0 -a bb:bb:bb:bb:bb:bb -c cc:cc:cc:cc:cc:cc wlp2s0mon
So here, it registers the function before the trap and then the trap can successfully be used, otherwise when the function was instead after the aireplay-ng line, I got this error:
Line 1: trp: command not found
For those wondering, it's just a script to bounce my nephew off the wifi when it's midnight so that he gets off TikTok and goes to sleep.
You should use trap true 2 or trap : 2 instead of trap '' 2. That's what "help trap" in a bash shell says about it:
If ARG is the null string each SIGNAL_SPEC is ignored by the shell and by the commands it invokes.
Example:
$ cat /tmp/test
#! /bin/sh
trap : INT
cat
echo first cat killed
cat
echo second cat killed
echo done
$ /tmp/test
<press control-C>
^Cfirst cat killed
<press control-C>
^Csecond cat killed
done
You can reset a trap to its default by giving the trap command - as its action argument. If you do this in a subshell, it won't affect the trap in the parent shell. In your script, you can do this for each command that you need to be interruptible with Ctrl-C:
#!/bin/bash
# make the shell (and its children) ignore SIGINT
trap '' INT
.
.
.
# but this child won't ignore SIGINT
(trap - INT; my_program)
# the rest of the script is still ignoring SIGINT
.
.
.