Linux Reader 4.5Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows
Linux Reader 4.5
Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows

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.

FREE DOWNLOADVer 4.5, Win Upgrade to PROFrom $29.95

0.45165085792542