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
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u