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
$ echo $?

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

$ ping
$ echo $?

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

$ cat test
echo Hello, World
exit 12
$ ./test
Hello, World
$ echo $?

No comments:

Post a Comment