LFCS · Question #3
Which of the following commands puts the output of the command date into the shell variable mydate?
The correct answer is A. mydate="$(date)". To assign the output of a command to a shell variable, command substitution syntax, typically $(command), is used.
Question
Options
- Amydate="$(date)"
- Bmydate="exec date"
- Cmydate="$((date))"
- Dmydate="date"
- Emydate="${date}"
How the community answered
(33 responses)- A91% (30)
- C3% (1)
- D3% (1)
- E3% (1)
Why each option
To assign the output of a command to a shell variable, command substitution syntax, typically `$(command)`, is used.
The `$(date)` syntax performs command substitution, which executes the `date` command and captures its standard output. This output is then assigned as the value to the `mydate` shell variable.
`mydate="exec date"` would attempt to replace the current shell with the `date` command, and its output would not be captured into the `mydate` variable.
`mydate="$((date))"` uses arithmetic expansion, which is designed for evaluating mathematical expressions, not for executing commands and capturing their string output.
`mydate="date"` assigns the literal string "date" to the variable `mydate` rather than executing the `date` command and capturing its output.
`mydate="${{date}}"` is not a valid shell syntax for command substitution or variable assignment.
Concept tested: Shell command substitution
Source: https://www.gnu.org/software/bash/manual/bash.html#Command-Substitution
Topics
Community Discussion
No community discussion yet for this question.