How to Exit From Bash Script

Muhammad Husnain Feb 02, 2024
  1. What is Bash Script
  2. When to Avoid the Use of Bash Script (i.e., Limitations)
  3. How to Exit from a Script
How to Exit From Bash Script

This article briefly introduces Bash scripting and discusses exiting a Bash script in case of some error. It further discusses the limitations and benefits of Bash scripting.

What is Bash Script

A computer script/program tells the computer what to do and say. Similarly, a Bash script is a sequence of commands that guide the Bash on what operations to perform.

A Bash script is a simple text file that contains a set of commands to execute. This command set can be a sequenced combination of many command-line instructions that we can write directly on the Bash interface (e.g., ls, cd, rm, etc.).

Any command executed from the command line prompt can also be put into the script and yield the same result as it would have on the command line prompt. Similarly, each command in a script file can also be fed directly into the Bash command-line interface, and the results will be the same.

There are no additional adjustments needed. You only need to type the commands according to their syntax, and they will give the same results.

Instead of putting them at the command line, we’re typing them into a simple text file.

The extension .sh is commonly used with Bash programs (e.g., loopScript.sh). As you may know, Linux is an extension-free operating system; therefore, the extension isn’t mandatory for a script to work properly.

More on creating your first executable Bash Script file can be found here.

When to Avoid the Use of Bash Script (i.e., Limitations)

There are certain cases where Bash scripting should be avoided. Some of them are:

  1. Tasks that need a lot of resources, especially when speed is the main focus (sort functions, hash functions, recursive functions, etc.)
  2. Functions that need a lot of Math operations, such as floating-point arithmetic, or that use complex numbers for calculations (instead, use C++ or FORTRAN). C or Java should be used when creating portable applications between different platforms.
  3. Structured programming is required for complex applications.
  4. Business-critical programs on which you stake your company’s future.
  5. Situations where security is critical, such as when you need to ensure your system’s integrity and safeguard against intrusion, cracking, or damage.
  6. The project is made up of interconnected subcomponents.
  7. File activities on a large scale are required. (Bash can only access serial files in a line-by-line manner.)
  8. Multi-dimensional arrays require native support.
  9. Data structures, such as queues or graphs, are required.
  10. Need to create or manage visuals or graphical user interfaces.
  11. Interact with hardware or external peripherals is required.
  12. I/O ports or sockets are required.
  13. You need to use built-in libraries or some old legacy code.
  14. Closed-source, proprietary applications (Shell scripts make the source code available to anyone who wants to look at it.)

How to Exit from a Script

There are some cases, such as we have written a script to test some code and we need to exit the script in case the code fails, then we can use the exit command in the script. A script is terminated with the exit command, much like a C program.

The exit command can also be used for inter-process communication (i.e., the argument passed to the exit command is returned to the script’s parent process).

Every command produces a success or failure status (a return status or an exit code). A successful command returns 0; an unsuccessful command produces a non-zero value, commonly understood as an error code.

On successful completion, most well-behaved UNIX commands, programs, and utilities return a 0 exit code, while there are also a few exceptions.

A script’s functions and the script itself both return an exit status. The exit status is determined by the function or script’s last command.

The syntax for the exit command is:

exit nn

An exit nn command can be used within a script to provide the shell an nn exit status (nn must be an integer in the 0 - 255 range). The exit status is the exit status of the last command executed in the script when it terminates with an exit with no parameter (previous to the exit).

Example:

#!/bin/bash

FIRST COMMAND
..
..
LAST COMMAND
# It will exit the script with the status of the last command.
exit

exit, exit $? or just omitting exit all have the same meaning: exiting with the last command’s status. After the script exits, we can check the status using the command echo $?.

Certain exit codes have some reserved meanings and should not be defined by the users. Such codes and their meanings are listed below.

Exit Code Description
1 Catch all general errors
2 if built-in shell functions are misused
126 the invoked command cannot be executed
127 The invoked command is not found
128 invalid arguments passed
130 script is terminated by using Ctrl+C keyboard keys
255* Out of range exit status
Muhammad Husnain avatar Muhammad Husnain avatar

Husnain is a professional Software Engineer and a researcher who loves to learn, build, write, and teach. Having worked various jobs in the IT industry, he especially enjoys finding ways to express complex ideas in simple ways through his content. In his free time, Husnain unwinds by thinking about tech fiction to solve problems around him.

LinkedIn

Related Article - Bash Exit