Machine-translated by Google. Questions are authored in English.
Top

Discussion

What happens when the command echo $0 is executed inside a shell script ?

  • A.Prints the last exit status
  • B.Prints the name of the shell executing the script
  • C.Prints the current working directory
  • D.Prints the value of the first argument to the script

Answer: B

The keyed answer is that it prints the name of the shell executing the script.

$0 is the shell's special parameter for the name of the program that is running. In an interactive shell, echo $0 prints the shell's own name, such as bash. Strictly, inside a script it holds the name by which the script was invoked, for example ./backup.sh; the options do not offer that, and the paper's option B is its description of $0 as the name of the running program.

The other options belong to other parameters and commands. The last exit status is $?. The current working directory is printed by pwd. The first argument to a script is $1, a common mix-up with $0.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.