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
- 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
- How to Use Shell Script Sleep Command
- Bash: How to Check if the File Does Not Exist
- How to use bash get script directory in Linux
- 20 Examples of Bash Find Command
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Linux: Sudo in Bash Scripts
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- Bash and sh: is there any difference
- A Bash Dirname Command
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- A Bash Nested Loop
- Use a Shell Script to Send an Email
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- Learn about useful bash scripts
- About chaining bash commands
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- Learn about C shell script
- Learn about Korn shell scripting
- 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
- Copy command in shell scripts
- Install Oracle Database
- About a bash date command
- Guide in Pictures
- 5 Basic Shell Script Examples for Your First Script
- Bash Time Command on Linux
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 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
- Bash: how to split strings in Linux
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: $0 in a Shell Script
- A crontab service shell script
- A Bash Multiline Command
- Linux: A Bash Linter
- A Bash Test Command
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- AWK in a Bash Script
- Learn about systemd startup script
- Using a bash tee command
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Using /usr/bin/env command
- Whether bash waits for command to finish
- Using bash if 0
- Learn to run Perl script in Linux
- Examples of using the Expect
- Shell script usage
- Here is everything you need to know about Bash for Windows




