0
0
Bash Scriptingscripting~5 mins

Backticks and $() for command substitution in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is command substitution in bash scripting?
Command substitution lets you run a command and use its output as part of another command or variable assignment.
Click to reveal answer
beginner
How do you use backticks for command substitution?
Place the command inside backticks like this: `command`. Bash runs the command and replaces it with its output.
Click to reveal answer
beginner
What is the modern alternative to backticks for command substitution?
Use $(command) instead of backticks. It is easier to read and can be nested without confusion.
Click to reveal answer
intermediate
Why is $(command) preferred over backticks?
Because it improves readability, allows nesting commands easily, and avoids problems with special characters inside commands.
Click to reveal answer
beginner
Show an example of using command substitution to assign the current date to a variable.
Using backticks: today=`date` Using $(): today=$(date) Both store the current date output into the variable today.
Click to reveal answer
Which syntax is correct for command substitution in bash?
A`ls -l`
BNone of the above
C$(ls -l)
DBoth A and C
Why might you prefer $(command) over backticks?
AIt works only in newer bash versions
BIt is easier to nest commands
CIt uses less memory
DIt is faster to execute
What will this command do? today=`date`
AAssign the string 'date' to today
BRun the date command but do not assign anything
CAssign the output of the date command to today
DCause an error
Which of these is NOT a benefit of using command substitution?
ARun commands faster than normal
BStore command output in variables
CUse command output inside another command
DBuild dynamic commands
What happens if you nest backticks like this: `echo `date``?
AIt causes syntax errors or unexpected results
BIt works fine
CIt runs only the inner command
DIt runs only the outer command
Explain how to use command substitution in bash and why $(command) is preferred over backticks.
Think about readability and nesting commands.
You got /4 concepts.
    Write a bash script snippet that assigns the current date and the current user to variables using command substitution.
    Use the modern $() syntax for clarity.
    You got /3 concepts.