In some cases when writing a script you want to prevent the user from exiting the script with CTRL+C or CTRL+Z.
Here is how you could do that!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hello,
Let’s take the following script as an example:
With the above, we are expecting the user to enter their name and then greet them. However, the user could just press
CTRL+C
orCTRL+Z
and exit the script.To prevent that, we could use the
trap
command and trap specific term signals. For example,CTRL+Z
sends theSIGTSTP
signal, andCTRL+C
sendsSIGINT
.So if we wanted to prevent the user from exiting the script with
CTRL+Z
orCTRL+C
, we could use thetrap
command and trap those signals:In case that you want to get a list of all term signals, you could use the following command:
Output:
I personally prefer to use the numeric representation as follows:
I hope that this helps! Regards, Bobby