Difference between revisions of "The exit status of a command"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
Line 1: Line 1:
 
Each Linux command returns a status when it terminates normally or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sys admin.
 
Each Linux command returns a status when it terminates normally or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sys admin.
* Every Linux command executed by the shell script or user has an exit status.
+
* Every Linux command executed by the shell script or user, has an exit status.
 
* The exit status is an integer number.
 
* The exit status is an integer number.
 
* The Linux man pages stats the exit statuses of each command.
 
* The Linux man pages stats the exit statuses of each command.
Line 6: Line 6:
 
* A non-zero (1-255 values) exit status means command was failure.
 
* A non-zero (1-255 values) exit status means command was failure.
 
* You can use special shell variable called ? to get previously executed command. To print ? variable use the echo command (<code>echo $?</code>
 
* You can use special shell variable called ? to get previously executed command. To print ? variable use the echo command (<code>echo $?</code>
 +
==How Do I See Exit Status Of The Command?==
 +
Type the following command:
 +
<source lang="bash">date</source>
 +
To view exist status of date command, enter:
 +
<source lang="bash">echo $?</source>
  
  
 
[[Category:Conditional Execution]]
 
[[Category:Conditional Execution]]

Revision as of 19:50, 1 September 2009

Each Linux command returns a status when it terminates normally or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. For example, if tar command is unsuccessful, it returns a code which tells the shell script to send an e-mail to sys admin.

  • Every Linux command executed by the shell script or user, has an exit status.
  • The exit status is an integer number.
  • The Linux man pages stats the exit statuses of each command.
  • 0 exit status means the command was successful without any errors.
  • A non-zero (1-255 values) exit status means command was failure.
  • You can use special shell variable called ? to get previously executed command. To print ? variable use the echo command (echo $?

How Do I See Exit Status Of The Command?

Type the following command:

date

To view exist status of date command, enter:

echo $?