Little Girl's Mostly Linux Blog

ExitScriptGracefully

Exit script gracefully

This page was last updated on September 06, 2013.

There are many ways to exit a script. Below are a few ways to do so gracefully.

Wait for key press and exit

This script prompts you to press any key, waits for you to do so, and then exits the script when you do.

#!/bin/bash
echo -n "Press any key to close this window.";
read keypress;
exit;

Wait for key press and display an interactive response and exit

This script prompts you for a response, waits for your response, reads the response, displays a comment containing the response, and then exits.

#!/bin/bash
echo -n "Type your name to close terminal window: "
read name;
echo "Goodbye, $name. I hope you enjoyed this script.";
exit;

Wait for key press and display an interactive response and pause and exit

This script prompts you for a response, waits for your response, reads the response, displays a comment containing the response, pauses for 2 seconds, and then exits.

#!/bin/bash
echo -n "Type your name to close terminal window: "
read name;
echo "Goodbye, $name. I hope you enjoyed this script.";
sleep 2;
exit;

See also

  • For more information on echo and how to use it in Bash scripts, type man echo into a terminal window and press the Enter key to see the echo manual.
  • For more information on read and how to use it in Bash scripts, type man read into a terminal window and press the Enter key to see the read manual.
  • For more information on exit codes and how to use exit in Bash scripts, see Exit and Exit Status.

Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: