nerdexam
LPI

102-500 · Question #82

Which of the following commands puts the output of the command date into the shell variable mydate?

The correct answer is D. mydate="$(date)". Option D uses command substitution - the $(...) syntax tells the shell to execute the command inside and replace the expression with its output, so mydate="$(date)" runs date and stores its output in mydate. Why the others fail: A assigns the literal string "date" - no…

Shells and Shell Scripting

Question

Which of the following commands puts the output of the command date into the shell variable mydate?

Options

  • Amydate="date"
  • Bmydate="exec date"
  • Cmydate="$((date))"
  • Dmydate="$(date)"
  • Emydate="${date}"

How the community answered

(57 responses)
  • A
    4% (2)
  • B
    2% (1)
  • C
    11% (6)
  • D
    82% (47)
  • E
    2% (1)

Explanation

Option D uses command substitution - the $(...) syntax tells the shell to execute the command inside and replace the expression with its output, so mydate="$(date)" runs date and stores its output in mydate.

Why the others fail:

  • A assigns the literal string "date" - no execution happens.
  • B assigns the literal string "exec date" - exec is a shell builtin, but wrapping it in quotes just makes it a string.
  • C uses $((...)) which is arithmetic expansion, not command substitution - it evaluates math expressions, not commands.
  • E uses ${date} which is variable expansion - it reads the value of a variable named date, not the date command.

Memory tip: Think of $( ) as "run this and give me the result" - the parentheses are like a container that executes, while quotes store literally and ${ } reads a variable. If you want a command's output, you need the command substitution form: $(command).

Topics

#command substitution#shell variables#bash syntax#process output

Community Discussion

No community discussion yet for this question.

Full 102-500 Practice