102-500 practice question 16 of 30
Which of the following commands puts the output of the command date into the shell variable mydate?
Full set: 233 questionsTo capture the output of a command and assign it to a variable, Bash uses command substitution. The modern and recommended syntax for command substitution is $(command). The shell executes the command within the parentheses and substitutes its standard output in its place. The statement mydate="$(date)" executes the date command, captures its output, and assigns it to the shell variable mydate.
B. This assigns the literal string "exec date" to the variable mydate, not the output of the date command.
C. The $((...)) syntax is for arithmetic expansion, not command substitution. It would cause a syntax error with date inside.
D. This assigns the literal string "date" to the variable mydate, rather than executing the command.
E. The ${...} syntax is for parameter expansion, which retrieves the value of a variable named date, not for executing a command.
1. GNU Bash Manual, Section 3.5.4 "Command Substitution". It describes the $(command) syntax, stating, "Bash performs the expansion by executing command in a subshell environment and replacing the command substitution with the standard output of the command".
2. The Open Group Base Specifications Issue 7, 2018 edition, IEEE Std 1003.1-2017, Section 2.6.3 "Command Substitution". This standard defines the $(command) syntax for POSIX-compliant shells.
Keep practicing
Take the free 102-500 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 102-500 practice questions
- Question 1What output is produced by the following command sequence? echo ‘1 2 3 4 5 6’ | while rea…
- Question 2Which command will set the local machine's timezone to UTC?
- Question 3Which of the following SQL statements will select the fields name and address from the co…
- Question 4Which of the following commands can remove a user from a group?
- Question 5Where are user specific crontabs stored?
- Question 6Which of the following crontab entries will execute myscript at 30 minutes past every hou…
