Exit Bash Script
This article provides a brief introduction to Bash scripting and discusses exiting a Bash script when an error occurs. It further discusses the limitations and benefits of Bash scripting.
What is Bash Scripting
A computer script/program tells a computer what to do and say. Similarly, a Bash script is a series of commands that instruct Bash what to do.
A Bash script is a simple text file that contains a set of commands to be executed. This command set can be a sequential combination of many command line instructions (such as ls, cd, rm, etc.) that we can write directly on the Bash interface.
Any command that you would execute from a command prompt can also be placed in a script and produce the same results as if it were executed from a command prompt. Likewise, every command in a script file can also be entered directly into the Bash command line interface with the same results.
No additional tuning is required. You just need to enter the commands according to the syntax and they will give the same results.
Instead of putting them on the command line, we'll enter them into a simple text file.
The extension .sh is commonly used for Bash programs (for example, loopScript.sh). As you may know, Linux is an extension-free operating system; therefore, extensions are not required for scripts to work properly.
When to Avoid Bash Scripts (i.e. Limit)
There are some situations where you should avoid using Bash scripts. Some of them are:
- Resource-intensive tasks, especially when speed is a primary concern (sorting functions, hashing functions, recursive functions, etc.)
- Functions that require extensive math, such as floating point operations, or calculations with complex numbers (rather than using C++ or FORTRAN). When creating portable applications between different platforms, you should use C or Java.
- Complex applications require structured programming.
- Key business plans for the company's future.
- Situations where security is critical, such as when you need to ensure the integrity of your system and prevent intrusion, hacking, or corruption.
- The project consists of interrelated subcomponents.
- Requires large-scale file activity. (Bash can access files only serially, line by line.)
- Multidimensional arrays require native support.
- A data structure such as a queue or a graph is required.
- Need to create or manage visual effects or graphical user interfaces.
- Need to interact with hardware or peripherals.
- An I/O port or socket is required.
- You need to use a built-in library or some old legacy code.
- Closed-source proprietary applications (Shell scripts make the source code available to anyone who wants to look at it.)
How to exit the script
There are some situations, such as we have written a script to test some code, and need to exit the script in case the code execution fails, then we can use the exit command in the script. The script terminates with the exit command, much like a C program.
exit
Commands can also be used for inter-process communication (ie, the arguments passed to the exit command are returned to the script's parent process).
Each command generates a success or failure status (return status or exit code). A successful command returns 0; an unsuccessful command generates a non-zero value, usually interpreted as an error code.
Most well-behaved UNIX commands, programs, and utilities return an exit code of 0 upon successful completion, but there are some exceptions.
Both the script's functions and the script itself return an exit status. The exit status is determined by the last command in the function or script.
The syntax of the exit command is:
exit nn
You can use the exit nn command in a script to provide the shell with an exit status of nn (nn must be an integer in the range 0 - 255). The exit status is the exit status of the last command executed in the script if it terminated with an exit without arguments (before exiting).
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
, has the same meaning: exit with the status of the last command. After the script exits, we can echo $?
check the status with the command .
Some exit codes have reserved meanings and should not be defined by the user. These codes and their meanings are listed below.
Exit Codes | illustrate |
---|---|
1 | Catch all general errors |
2 | If the built-in shell function is abused |
126 | The command called cannot be executed |
127 | The command invoked was not found |
128 | An invalid parameter was passed |
130 | The script is terminated by using the Ctrl+C keyboard keys |
255* | Out of range exit status |
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if input parameter exists in Bash
Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM
-
When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh
timeout command in Bash
Publish Date:2025/03/21 Views:105 Category:OPERATING SYSTEM
-
This article is a simple guide to setting a timeout for a specific program using the timeout command from GNU's coreutils package in Bash. timeout command in Bash In many cases, such as fetching data from a server or running a function or p
Start a new terminal session in Bash
Publish Date:2025/03/21 Views:51 Category:OPERATING SYSTEM
-
In various situations when using Bash or another shell, you may need to run a script or program in a new terminal instance or in another tab in the same terminal. Opening a new terminal instance or tab from within Terminal is simple; we'll
Checking Syntax in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
This article discusses methods for checking Bash scripts for syntax problems without running the script. Syntax errors are caused by some grammatical or spelling mistakes in the code. In modern compilers of other programming languages, the
Run batch (.bat) files in CMD
Publish Date:2025/03/20 Views:155 Category:OPERATING SYSTEM
-
This article will show you how to use CMD to run a batch file.bat. There are three ways to run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way you can just go
Changing CMD text color using batch script
Publish Date:2025/03/20 Views:172 Category:OPERATING SYSTEM
-
This article will first discuss the basic concepts of batch scripts or batch files. After introducing the Batch script, let's discuss how to use the Batch script to change the text color of CMD every second. Batch script or file A batch scr
Batch set command timeout
Publish Date:2025/03/20 Views:50 Category:OPERATING SYSTEM
-
This article will first discuss the concept of timeout command in batch scripting. After that, we will discuss setting timeout command for any other command. Timeout command in batch script A timeout is a utility that pauses or delays for a
Batch merge XML files
Publish Date:2025/03/20 Views:101 Category:OPERATING SYSTEM
-
This article will first discuss and understand the XML file format. After that, we will discuss merging two or more XML files into one file using batch commands and scripts. XML File XML, also known as Extensible Markup Language, is a marku
Run .exe file from command prompt using batch script
Publish Date:2025/03/20 Views:77 Category:OPERATING SYSTEM
-
This article will show you how to use a batch (.bat) script to run a .exe type file. You can use two different commands to achieve this. Let’s discuss each method in the following sections. Run .exe file from command prompt using title an