In this post I am sharing a small and useful tip about, how to know last command run successfully in Linux and Unix .The post will explain ,to get the exit status of command used last time. As a System Engineer, sometimes I have to check the success status of last command I run on terminal.It is not only with command but you can apply this for script also.
To know the exit status of last command, run below given command
You will get the output in integer.
If output is ZERO ( 0 ), it means command has been run successfully. Whereas if output of the command is NON ZERO then command was not run successfully.
See given below example
linux@tuxworld:/tmp$ ls acroread_1002_1002 orbit-linux plugtmp-2 pulse-PKdhtXMmr18n unity_support_test.0 kde-linux plugtmp plugtmp-3 ssh-RRoiHGlxeCVF ksocket-linux plugtmp-1 pulse-EfTYmbAMhCGe tmpQA7rpT linux@tuxworld:/tmp$ linux@tuxworld:/tmp$ echo $? 0 linux@tuxworld:/tmp$ linux@tuxworld:/tmp$ lsla lsla: command not found linux@tuxworld:/tmp$ linux@tuxworld:/tmp$ echo $? 127 linux@tuxworld:/tmp$ linux@tuxworld:/tmp$
In above given example, you can see after using
ls command,I used
echo $? command.Its output is ZERO ,it means command run successfully.
Whereas,on after running false command lsla,the
echo $? command output is 127. Which is NON ZERO,hence the command was not run successfully