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.
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)- A71% (34)
- B4% (2)
- C6% (3)
- D17% (8)
- E2% (1)
Why each option
Backtick command substitution executes a command and assigns its output as a variable value in bash.
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.
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.
The pipe operator passes the stdout of one command as stdin to the next command - it cannot assign output to a shell variable.
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.
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
Community Discussion
No community discussion yet for this question.