What is a Bash Exit Status of Last Command?
If you work with different scripts for automation and monitoring purposes, some essential bash scripting commands will help improve your productivity. Bash status of last command is quite an important command for bash scripters.
Here you will find out:
- what a bash exit status of last command is
- how to use an exit code in a shell script and how to set it
- when DiskInternals can help you
Are you ready? Let's read!
The exit code of the last command you run
For every Linux command you run on a shell, it must return an exit status. Usually, the exit status is represented with an integer number, where 0 means that the script/command worked successfully without errors. Any other number besides zero (0) means there was an error in the script or command.
An exit code or return code is important because it helps to identify if a script or command can be further used for other purposes. When an exit code is not set, the exit code is the exit code of the last command you run.
To get the exit code of a command
If you want to get the bash result of a last command, you have to introduce the $? variable after the “echo” command. Alternatively, you can use the printf command:
If the code doesn’t run properly, the exit code will be 1 or any other number between 1 – 254.
Using an exit code in a shell script
You have seen how to get an exit code of shell scripts. Now, let's see how you can use an exit code inside a script. You can use the $? special variable inside a shell script to check for exit status.
From the script above, if the exit code is 0, the script will echo a successful message, but if the exit code is any other number, the script will echo a failure message.
Set an exit code
If you want to set an exit code in a script, here’s how to do that. However, you should note that the exit command can cause normal shell script termination.
Here the exit code is defined as 1.
If you want to open Linux files in Windows
So you booted to Windows on your dual-boot PC, and you need to access some files saved on a Linux partition. What should you do? DiskInternals Linux Reader is an intuitive software app that will help you out. It provides you with access to files on Ext2/3/4, ReiserFS, Reiser4, HFS, HFS+, FAT, exFAT. DiskInternals Linux Reader is free to download and use. Plus, it features an easy to navigate interface. Also, this software works for virtual machine users, and it is very effective for opening Linux files on Windows.