Which command will display the exit status code on the screen?

Chapter 6. Exit and Exit Status

 

... there are dark corners in the Bourne shell, and people use all of them.

--Chet Ramey

The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script's parent process.

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.

Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).

Which command will display the exit status code on the screen?

When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to the exit).

#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exit

The equivalent of a bare exit is exit $? or even just omitting the exit.

#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exit $?

#!/bin/bash COMMAND1 . . . COMMAND_LAST # Will exit with status of last command.

$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash's way of giving functions a "return value." [1]

Following the execution of a pipe, a $? gives the exit status of the last command executed.

After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.

Example 6-1. exit / exit status

#!/bin/bash echo hello echo $? # Exit status 0 returned because command executed successfully. lskdf # Unrecognized command. echo $? # Non-zero exit status returned -- command failed to execute. echo exit 113 # Will return 113 to shell. # To verify this, type "echo $?" after script terminates. # By convention, an 'exit 0' indicates success, #+ while a non-zero exit value means an error or anomalous condition. # See the "Exit Codes With Special Meanings" appendix.

$? is especially useful for testing the result of a command in a script (see Example 16-35 and Example 16-20).

Which command will display the exit status code on the screen?

The !, the logical not qualifier, reverses the outcome of a test or command, and this affects its exit status.

Example 6-2. Negating a condition using !

true # The "true" builtin. echo "exit status of \"true\" = $?" # 0 ! true echo "exit status of \"! true\" = $?" # 1 # Note that the "!" needs a space between it and the command. # !true leads to a "command not found" error # # The '!' operator prefixing a command invokes the Bash history mechanism. true !true # No error this time, but no negation either. # It just repeats the previous command (true). # =========================================================== # # Preceding a _pipe_ with ! inverts the exit status returned. ls | bogus_command # bash: bogus_command: command not found echo $? # 127 ! ls | bogus_command # bash: bogus_command: command not found echo $? # 0 # Note that the ! does not change the execution of the pipe. # Only the exit status changes. # =========================================================== # # Thanks, St�phane Chazelas and Kristopher Newsome.

Which command will display the exit status code on the screen?

Certain exit status codes have reserved meanings and should not be user-specified in a script.

When a command finishes execution, it returns an exit code. The exit code is not displayed on the screen by default. To examine the exit code, you need to examine a special variable, "$?"

Say, you are searching for a string in a text file.

$ grep x1y2z3 somefile.txt $

The standard output of the command returns null, which is a pretty good indication that the string cannot be found in the file.

But what if you embed the grep command in a script? How can you tell if the string is found or not?

Checking the exit code will tell you. Let's first try it out interactively.

$ grep x1y2z3 somefile.txt $ echo $? 1

Note that in bash, the exit status is 0 if the command succeeded, and 1 if failed. For grep, 0 means that the string was found, and 1 (or higher), otherwise.

To check the exit status in a script, you may use the following pattern:

somecommand argument1 argument2 RETVAL=$? [ $RETVAL -eq 0 ] && echo Success [ $RETVAL -ne 0 ] && echo Failure

What is the command to check exit code?

To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command. $ echo $?

What command should I use to display the exit code of the previous command?

The echo command is used to display the exit code for the last executed Fault Management command.

What is the command to check exit in Linux?

“$?” is a variable that holds the return value of the last executed command. “echo $?” displays 0 if the last command has been successfully executed and displays a non-zero value if some error has occurred. The bash sets “$?” To the exit status of the last executed process.

What is use of exit and exit status command?

The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script's parent process. Every command returns an exit status (sometimes referred to as a return status or exit code).