bash – get previous command status

 

To get previous command status in bash use “$?” variable.

echo $? will return zero if previous command is success & if its non zero it indicates failure.

Example:

$ ls ; echo $?

Output of the above command is list of files, directories and next line it will return number.

We can use condition for check if previous command is success or failure:

any_command
if [ $? -eq 0 ] 
then
    echo SUCCESS
else
    echo FAIL
fi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s