XK0-004 · Question #201
An administrator notices the HISTSIZE variable is 50, using the commands below: HISTSIZE=50 export HISTSIZE The administrator rechecks the HISTSIZE value using echo HISTSIZE but gets no value. Which o
The correct answer is A. printenv | grep $HISTSIZE. Using echo HISTSIZE without a dollar sign prints the literal string rather than the variable's value; printenv lists exported variables with their values and can be filtered to retrieve them.
Question
An administrator notices the HISTSIZE variable is 50, using the commands below:
HISTSIZE=50 export HISTSIZE The administrator rechecks the HISTSIZE value using echo HISTSIZE but gets no value. Which of the following commands should the administrator use to retrieve its value?
Options
- Aprintenv | grep $HISTSIZE
- Becho HISTSIZE
- Cprintf HISTSIZE
- Dgrep $HISTSIZE
How the community answered
(40 responses)- A83% (33)
- B3% (1)
- C5% (2)
- D10% (4)
Why each option
Using `echo HISTSIZE` without a dollar sign prints the literal string rather than the variable's value; `printenv` lists exported variables with their values and can be filtered to retrieve them.
`printenv` outputs all currently exported environment variables in `NAME=value` format. Piping to `grep $HISTSIZE` causes the shell to expand the variable first (to 50), so the effective command is `grep 50`, which matches and displays the line `HISTSIZE=50` from `printenv` output. Because the variable was explicitly exported, it appears in `printenv`'s output, making this the only option that reliably retrieves the value.
`echo HISTSIZE` prints the literal string 'HISTSIZE' because no `$` prefix is used to trigger variable expansion, which is the exact mistake the administrator already made.
`printf HISTSIZE` also treats the argument as a literal format string with no variable expansion, printing 'HISTSIZE' rather than its value.
`grep $HISTSIZE` without a file argument or pipe source waits for standard input and never searches environment variables, so it will not return the variable's definition.
Concept tested: Retrieving exported shell environment variable values
Source: https://www.gnu.org/software/bash/manual/bash.html#index-printenv
Topics
Community Discussion
No community discussion yet for this question.