Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com
huuphan.com. Powered by Blogger.

Check exit codes in bash

In this tutorial, How to check exit codes in bash Linux. You can not see the numeric codes unless you want to. If you're going to see the numeric exit code use echo $? command.

 For example check exit codes in bash

 abc the command error message will tell you there is no "abc" script in your bin directory.

$ bin/abc

$ echo $?

 


On the Linux command line most commands the exit code will be 0.

$ pwd
$ echo $?



Example the exit code 1 as below:

$ ls -lR /usr > /dev/null 2>&1
$ echo $?



I start a second shell, exit it and then display the exit code.

$ bash            # start new shell
$ exit 123
exit
$ echo $?
123



If you ping a system which doesn’t exist on your network or isn’t responding for some reason as example below:
 

$ ping 192.168.1.111
$ echo $?



You can get the exit code with a particular code by include an exit command as below:

$ cat test
#!/bin/bash
echo Hello, World huuphan.com
exit 12
$ ./test
Hello, World huuphan.com
$ echo $?
12




post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com