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…
Question
Options
- Amydate="date"
- Bmydate="exec date"
- Cmydate="$((date))"
- Dmydate="$(date)"
- Emydate="${date}"
How the community answered
(57 responses)- A4% (2)
- B2% (1)
- C11% (6)
- D82% (47)
- E2% (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"-execis 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 nameddate, not thedatecommand.
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
Community Discussion
No community discussion yet for this question.