Amazon

Dec 18, 2009

Linux question: command or script exit status

Question: What is exit status of shell or command in Linux?


Answer:
Every time a process or shell exit, it will send a terminating code to the operating system. This is what they call 'exit status'. We can detect wether the command is successful or not by checking the exit status. Here's how you can check:


user@penguin$ echo "Hello World"
Hello World
user@penguin$ echo $?
0
user@penguin$


$? is a variable where Linux stores the exit status of the previous command it ran.
0 in exit code is successful. If greater than 0, it means that it was exited abnormally or there's an error.


user@penguin$ ech "Hello World"
-bash: ech: command not found
user@penguin$ echo $?
127
user@penguin$


ech is not a linux command. And the exit code is greater than 0 because of the error. 
Try to play with it. This will be a great help for your linux knowledge.


Feel free to post a comment.

No comments:

Post a Comment