Bash Catch Ctrl C In A Script

[Solved] Bash Catch Ctrl C In A Script | Basic - Code Explorer | yomemimo.com
Question : bash catch ctrl-c in a script

Answered by : plamen-grozdanov

#!/bin/bash
function finish() { echo "bye bye!"
}
trap finish SIGINT
for number in $(seq 10); do echo "TODO: Insert work here..." # Insert work to do here.
done

Source : https://askubuntu.com/questions/524525/cant-trap-ctrlc-in-my-simple-script | Last Update : Mon, 10 May 21

Question : bash catch ctrl-c in a script

Answered by : plamen-grozdanov

#!/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

Source : https://stackoverflow.com/questions/12771909/bash-using-trap-ctrlc | Last Update : Mon, 10 May 21

Answers related to bash catch ctrl c in a script

Code Explorer Popular Question For Basic