JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Exit Bash Script

Author:JIYIK Last Updated:2025/03/21 Views:

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:

  1. Resource-intensive tasks, especially when speed is a primary concern (sorting functions, hashing functions, recursive functions, etc.)
  2. 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.
  3. Complex applications require structured programming.
  4. Key business plans for the company's future.
  5. Situations where security is critical, such as when you need to ensure the integrity of your system and prevent intrusion, hacking, or corruption.
  6. The project consists of interrelated subcomponents.
  7. Requires large-scale file activity. (Bash can access files only serially, line by line.)
  8. Multidimensional arrays require native support.
  9. A data structure such as a queue or a graph is required.
  10. Need to create or manage visual effects or graphical user interfaces.
  11. Need to interact with hardware or peripherals.
  12. An I/O port or socket is required.
  13. You need to use a built-in library 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 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.

exitCommands 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.

Article URL:

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 –

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial