nerdexam
CompTIA

LX0-104 · Question #255

Which of the following commands will print the exit value of the previous command to the screen in bash?

The correct answer is A. echo $?. In bash, the special variable $? holds the exit status of the most recently executed foreground command.

Shells, Scripting and Data Management

Question

Which of the following commands will print the exit value of the previous command to the screen in bash?

Options

  • Aecho $?
  • Becho $#
  • Cecho $exit
  • Decho $status
  • Eecho $&}

How the community answered

(27 responses)
  • A
    96% (26)
  • D
    4% (1)

Why each option

In bash, the special variable `$?` holds the exit status of the most recently executed foreground command.

Aecho $?Correct

The special shell variable `$?` contains the exit status of the last executed foreground command, which `echo` will print to standard output.

Becho $#

`$#` contains the number of positional parameters passed to a script or function, not the exit status.

Cecho $exit

`$exit` is not a standard shell variable for the exit status; `exit` is a command.

Decho $status

`$status` is used in some shells like csh/tcsh for the exit status, but not in bash.

Eecho $&}

`$&}}` is not a valid or recognized shell variable or construct.

Concept tested: Shell special variables (exit status)

Source: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Special-Parameters

Topics

#bash variables#exit status#shell scripting

Community Discussion

No community discussion yet for this question.

Full LX0-104 Practice