Get exit status of a process in bash

You can simply do a echo $? after executing the command/bash which will output the exit code of the program.

Every command returns an exit status (sometimes referred to as a return
status or exit code). A successful command returns a 0, while an
unsuccessful one returns a non-zero value that usually can be interpreted
as an error code. Well-behaved UNIX commands, programs, and utilities return
a 0 exit code upon successful completion, though there are some exceptions.

# Non-zero exit status returned -- command failed to execute.
echo $?    
  
echo

# Will return 113 to shell.
# To verify this, type "echo $?" after script terminates.    
exit 113             

# By convention, an 'exit 0' indicates success,
# while a non-zero exit value means an error or anomalous condition

Alternately if you wish to identify return code for a background process/script (started with nohup or run in background & operator) you could get the pid of the process/script started and wait for it to terminate and then get the exit code.

# Some random number for the process-id is returned
./foo.sh &
[1] 28992          

# Get process id of the background process
echo $!          
28992 

# Waits until the process determined by the number is complete
wait 28992       

[1]+  Done         ./foo.sh

# Prints the return code of the process
echo $?          
0

Also take a look at Bash Hackers Wiki – Exit Status

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)