Linux Administration
medium

Questão de Linux Administration #019e3987

Enunciado da Questão
Linux Administration
#set
#bash-builtins
#shell-variables
#shell-functions
#env
Médio

Which of the following commands lists all defined variables and functions within Bash?

A

env

B

set

Correta
C

env -a

D

echo $ENV

Explicação Oficial / Explicações da Comunidade

The set command, when invoked without any options, displays the names and values of all shell variables and functions in the current Bash environment. This includes both environment variables and local shell variables, as well as any defined functions.

How it Works

  • set: In Bash, set is a built-in command that, with no arguments, outputs all shell variables (including environment variables) and functions. It shows the current shell state, making it useful for debugging and inspection.
  • env: This command lists only environment variables (exported variables) that are passed to child processes. It does not show local shell variables or functions.
  • env -a: The -a flag is not a valid option for env; env does not have a -a option. This command would likely produce an error or be ignored.
  • echo $ENV: This attempts to print the value of the variable ENV. Typically, ENV is not set by default in Bash, so this would output an empty line. It does not list all variables or functions.

Why the Correct Answer is Right

  • The set command is the standard way to list all defined variables and functions in a Bash shell. It provides a comprehensive view of the current shell environment.

Why the Incorrect Answers are Wrong

  • env: Only shows exported environment variables, missing local variables and functions.
  • env -a: Invalid option; env does not support -a. Even if it did, it would still only show environment variables.
  • echo $ENV: Only prints the value of a single variable (ENV), which is typically unset. It does not list all variables or functions.