What is an exit status or an error code in a bash script?
Here you will find out:
- what an exit status or an error code in a bash script is
- how to get the exit code
- what the standard bash error codes are
- when DiskInternals can help you
Are you ready? Let's read!
An error code in a bash script
An exit code or a return code results from a process executed on a shell in Linux/UNIX systems. Every Linux/UNIX command returns an exit status, which is represented by an exit code (a number ranging from 0 to 255). When a command returns an exit status “0,” it means the command was successfully executed. By contrast, if you get an exit code of “1” or any other number besides “0,” it means that your code didn’t run successfully.
If you want to get the exit code
To get the exit code of any command, use the “$?” variable alongside the echo command or printf command.
Example:
The “0” return code indicates that the command was successful. It implies that the file “intro.txt” exists and the Linux/UNIX system could read it without errors.
Another example:
The “127” error code indicates that the input command does not exist and could not be executed.
What are the standard bash error codes?
Here are the standard bash error codes you should know about. However, the Linux Documentation Project contains a list of all the reserved codes for Linux and UNIX systems.
1 - Catchall for general errors
2 - Misuse of shell built-ins
126 - Command invoked, cannot execute
127 - “Command not found”
128 - Invalid argument to exit
128+n - Fatal error signal “n”
130 - Script terminated by Control-C
255\* - Exit status out of range
Suppression of error output
In some cases, you may want to suppress an exit status, probably because the command is being executed within another script, and you don't want to get any other error code besides "0".
Use the command “2>/dev/null” to send standard error outputs to /dev/null to suppress an error code. Also, when you combine the OR operation and the suppressed error output, a non-existing file or command can return as “0”. The OR operation (|| exit 0) provides a fallback, outputting “0” as the error code even if a command is not successful.
Example:
Open Linux files with Linux Reader
If you’re working in Windows and need to access your Linux files on your dual-boot PC, DiskInternals Linux Reader is the best software to use. DiskInternals Linux Reader is a handy freeware program that lets you view files saved on Ext2/3/4 partitions or any Linux-compatible partition while operating in Windows. This software is best for virtual machine users and dual-boot PC users. Interestingly, DiskInternals Linux Reader supports a wealth of file systems, as well as FTP servers.
Related articles
- About a bash date command
- How to Access Linux Ext2 or Ext3 on Windows
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Mount Linux Drive on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Bash: how to split strings in Linux
- Linux: Sudo in Bash Scripts
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A crontab service shell script
- Bash and sh: is there any difference
- A Bash Dirname Command
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- A Bash Test Command
- Use a Shell Script to Send an Email
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- AWK in a Bash Script
- Learn about useful bash scripts
- Learn about systemd startup script
- About chaining bash commands
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Learn about Korn shell scripting
- Using /usr/bin/env command
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Shell script usage
- Install Oracle Database
- Here is everything you need to know about Bash for Windows
- Guide in Pictures
- How to Use Shell Script Sleep Command
- A Bash Multiline Command




