When writing Bash scripts, you will frequently need to stop the execution of a script when a certain condition is met or perform some actions based on a command's exit code.
In this thread I will go over the built-in bash exit command as well as the exit statuses of the commands that have been executed.
Exit Status
When a shell command exits, whether successfully without any errors or unsuccessfully with errors, it returns an exit code.
An exit code of zero indicates that the command was completed properly without any errors, while a non-zero indicates that an error occurred.
The $? is a special shell variable that stores the exit status of the most recently run command:
$ echo $?
Because the batcat command was completed successfully and without error, the exit code is zero, as expected.
If you attempt to run batcat command on a not-existing file, the exit code will be non-zero as shown below:
$ batcat nullfile
The command's status code can be used for debugging and to determine why it failed. The man pages for each command include information about the exit codes.
When chaining commands using pipes, the exist status code is that of the last command in the chain.
$ ls -lah | head -n 3
In the preceding example, echo $? prints the exit code of the head command and its zero. To prove this, execute the ls command on a non-existent directory.
Here you can see the ls command fails but we still get the status code of zero.
$ ls -lah nulldir | head -n e
The bash exist command:
Now that you know what command exist status codes are, let's look at how to set them using the exit command.
The exit command is used to exit the current shell. It takes a number as a parameter and exits the shell with the status of the number you passed as a parameter value.
If no parameters were supplied, it would return the status of the most recently executed command.
Here is the syntax for the exit command:
$ exit N
When the exit command is used in shell scripts, the value passed to it as an argument is returned to the shell as an exit code.
Here is an example script of using the exit command in shell scripts:
Always remember that if a script ends with exit without specifying a parameter, the script exit code is the last command executed in the script.
This is the same as just exit $? or leaving the script without exit at all.
This information should be sufficient to help you understand command exist status codes and the exit command.
That's all! Thank you for getting this far. I hope you find this thread useful.
If you did find this thread valuable:
1. Toss us a follow for more daily threads on Linux, sysadmin, and DevOps β @linuxopsys
2. Like and RT the first tweet so other Linux folks can find it too.
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
10 sed command practical examples that will make you a Linux power user:
A brief introduction on the sed command:
Sed (short for stream editor) is a powerful command-line tool used for text manipulation in Unix-like operating systems.
It provides a wide range of features to search, delete, find, substitute (find and replace), inserting, and appending text using regular expressions. In this thread, we will explore 15 sed command practical examples that will make you a Linux power user.
In Linux, most people use the ls command to check a file's creation, access, and modification times, ownership, and permissions.
What if I told you there is another great way to display detailed information about files and file systems?
Learn more about it in this thread:
The Linux ls command typically displays basic or chunk of information about a file; however, what if you want to print more information about the file? This is where the stat command comes in.
stat (short for status) is a command-line utility for displaying detailed information about specific files or file systems. It is commonly used to obtain file timestamps.
In addition to UGO/RWX permissions and ACLs (Access Control Lists), Linux uses file and directory attributes to control the level of access that system programs and users have to files.
Letβs dive in!
File and directory attributes can be set and removed using different commands and utilities. Let's explore some commonly used attributes and how to manage them.
1. Immutable Attribute:
The immutable attribute prevents a file or directory from being modified, renamed, or deleted, even by the root user. It provides an extra layer of security to critical system files or sensitive data.
When it comes to creating backups, packaging software source code for distribution, and managing files in Linux, the tar command is no doubt one of the widely used archiving utilities.
Learn more in this guide:
Tar command which is abbreviated as tape archive is used to group files into archives called tarballs and also compress files using popular compression algorithms such as gzip, bzip2, and xz.
Common Options
The picture below lists the most common options you can use with the tar command to perform different operations.
pushd and popd are very underutilized commands, yet incredibly powerful.
These cmds give you the ability to manage your directory stack and easily switch between directories, making them a must-have tool for any Linux user looking to maximize their efficiency and productivity
In this thread, I'll show you how to use the pushd and popd commands to unlock the power of easy system directory navigation and streamline your workflow.
First let's understand what a Directory Stack is?
The Directory Stack is an ordered list of directories that have been accessed during navigation.
The contents of the Directory Stack can be viewed through the execution of the dirs command.
If you want to learn Linux bash scripting for FREE, open this:
A collection of comprehensive bash scripting guides.
1. What is Shell in Linux
In this guide, let's learn about the shell which is an important part of the Linux operating system which interprets and passes the commands you provide on your keyboard to the operating system to perform some specific tasks
A variable is a named storage location in a program's memory where a value can be stored, retrieved, and manipulated. Like any programming language, Variables are an essential component in the Bash scripts.