Math-Linux.com

Knowledge base dedicated to Linux and applied mathematics.

Home > Linux > Bash > How to check exit status of a command line in BASH ?

How to check exit status of a command line in BASH ?

All the versions of this article: [English] [français]

$? Operator gives the exit status code

To check the exit status in bash, you can user the specific operator $?, for example here, we check if there is an ldaps server(not ldap but ldaps !!!)

root@kali:~# cat /etc/ldap/ldap.conf 
BASE        dc=example,dc=com
URI        ldap://ldap.example.com ldap://ldap-master.example.com:666
TLS_CACERT        /etc/ssl/certs/ca-certificates.crt
root@kali:~# grep ldaps /etc/ldap/ldap.conf

Next we define, the exit STATUS using operator $?:

root@kali:~# STATUS="$?"
root@kali:~# [ "$STATUS" -eq 0 ] && echo "STATUS OK"
root@kali:~# [ "$STATUS" -ne 0 ] && echo "STATUS KO"
STATUS KO

STATUS equal to 0 means a success (failure if different to 0). In our case, string ldaps is not in ldap.conf, it is a failure.

Any message or comments?

comments powered by Disqus