nerdexam
LPI

010-100 · Question #10

The output of the program date should be saved in the variable actdat. What is the correct statement?

The correct answer is A. actdat=`date`. Backtick command substitution executes a command and assigns its output as a variable value in bash.

Development

Question

The output of the program date should be saved in the variable actdat. What is the correct statement?

Options

  • Aactdat=date
  • Bset actdat='date'
  • Cdate | actdat
  • Ddate > $actdat
  • Eactdat=date

How the community answered

(48 responses)
  • A
    71% (34)
  • B
    4% (2)
  • C
    6% (3)
  • D
    17% (8)
  • E
    2% (1)

Why each option

Backtick command substitution executes a command and assigns its output as a variable value in bash.

Aactdat=`date`Correct

The syntax actdat=`date` uses backtick command substitution, which executes the date command in a subshell and assigns its stdout as the value of actdat. Variable assignment in bash requires no spaces around the equals sign, and wrapping the command in backticks (or the equivalent $()) is the correct way to capture command output.

Bset actdat='date'

The set built-in modifies shell options or positional parameters, not named variables; additionally, 'date' in single quotes is treated as a literal string, not a command to execute.

Cdate | actdat

The pipe operator passes the stdout of one command as stdin to the next command - it cannot assign output to a shell variable.

Ddate > $actdat

The > operator redirects output to a file, not into a variable; $actdat would expand to the current value of actdat as a filename rather than storing output into the variable.

Eactdat=date

Without backticks or $(), actdat=date assigns the literal string 'date' to the variable instead of the output of executing the date command.

Concept tested: Bash command substitution for variable assignment

Source: https://www.gnu.org/software/bash/manual/bash.html#Command-Substitution

Topics

#command substitution#bash variables#shell scripting#date command

Community Discussion

No community discussion yet for this question.

Full 010-100 Practice