nerdexam
LPI

010-150 · Question #63

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`. To capture the output of a command into a shell variable, command substitution must be used - either backtick syntax or $().

The Power of the Command Line

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

(30 responses)
  • A
    70% (21)
  • B
    17% (5)
  • C
    3% (1)
  • D
    3% (1)
  • E
    7% (2)

Why each option

To capture the output of a command into a shell variable, command substitution must be used - either backtick syntax or `$()`.

Aactdat=`date`Correct

Backtick syntax (`` actdat=`date` ``) is command substitution, which executes `date` and assigns its standard output as a string to the variable `actdat`. This is one of the two valid forms of command substitution in POSIX shell (the other being `actdat=$(date)`).

Bset actdat='date'

`set actdat='date'` uses single quotes, so `date` is treated as a literal string, not executed; `set` also does not assign named variables this way.

Cdate | actdat

`date | actdat` attempts to pipe the output of `date` to a command named `actdat`, which is not a valid command.

Ddate > $actdat

`date > $actdat` redirects the output of `date` to a file whose name is the current (likely empty) value of `$actdat`, rather than assigning the output to the variable.

Eactdat=date

`actdat=date` assigns the literal string `date` to the variable, not the output of the `date` command.

Concept tested: Shell command substitution to capture command output

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

Topics

#command substitution#shell variables#backticks#shell scripting

Community Discussion

No community discussion yet for this question.

Full 010-150 Practice