Frank Joe Frank Joe - 1 year ago 87
Bash Question

How to prevent a user from using ctrl-c to stop a script?

I load a script in .bash_profile and this script will ask for right password whenever a user opens a terminal window. If the user enters a wrong code, the script will run


to stop the current terminal.

if [ code = "980425" ]; then
echo hello

But I realize that the user can always use


to stop the script and enter the terminal.
How to avoid that?

Answer Source

You can always trap SIGINT:

trap 'echo got SIGINT' SIGINT

Once you're done, reinstall the default handler again with


See the POSIX spec for trap for details. This works in all Bourne shells, not just bash.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download