0
0
Bash Scriptingscripting~10 mins

Backticks and $() for command substitution in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Backticks and $() for command substitution
Start script
Run command inside backticks or $()
Shell executes command
Command outputs text
Shell replaces substitution with output
Use output in script
Continue script execution
The shell runs the command inside backticks or $(), captures its output, and replaces the substitution with that output for use in the script.
Execution Sample
Bash Scripting
current_date=`date`
echo "Today is $current_date"
This script stores the output of the date command in a variable and then prints it.
Execution Table
StepActionCommand/SubstitutionOutput CapturedVariable StateEcho Output
1Execute command substitution`date`Fri Jun 14 12:00:00 UTC 2024current_date = 'Fri Jun 14 12:00:00 UTC 2024'
2Echo variableecho "Today is $current_date"current_date unchangedToday is Fri Jun 14 12:00:00 UTC 2024
3End scriptcurrent_date unchanged
💡 Script ends after printing the date stored in current_date.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
current_dateundefinedFri Jun 14 12:00:00 UTC 2024Fri Jun 14 12:00:00 UTC 2024Fri Jun 14 12:00:00 UTC 2024
Key Moments - 3 Insights
Why do we use backticks or $() around a command?
Because the shell runs the command inside them and replaces it with the command's output, as shown in execution_table step 1.
What happens if the command inside backticks produces multiple lines?
All output lines are captured as a single string with newlines preserved, and assigned to the variable, just like in step 1.
Is there a difference between using backticks and $()?
Both do command substitution, but $() is easier to read and nest, which helps avoid confusion in complex scripts.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of current_date after step 1?
A"date"
B"Fri Jun 14 12:00:00 UTC 2024"
C"Today is Fri Jun 14 12:00:00 UTC 2024"
Dundefined
💡 Hint
Check the 'Variable State' column in row for step 1 in execution_table.
At which step does the script print the final output?
AStep 2
BStep 3
CStep 1
DNo output is printed
💡 Hint
Look at the 'Echo Output' column in execution_table.
If we replace backticks with $(), how does the execution table change?
AEcho output changes to command name
BVariable current_date becomes empty
CNo change in output or variable state
DScript fails to run
💡 Hint
Both backticks and $() perform command substitution similarly, as explained in key_moments.
Concept Snapshot
Command substitution runs a command inside backticks (`command`) or $() and replaces it with the output.
Use it to capture command output into variables.
Example: current_date=`date` or current_date=$(date).
$() is preferred for readability and nesting.
The shell replaces the substitution before running the rest of the script.
Full Transcript
In bash scripting, backticks (`command`) and $() are used for command substitution. This means the shell runs the command inside them and replaces the whole expression with the command's output. For example, current_date=`date` runs the date command and stores its output in the variable current_date. Then echo "Today is $current_date" prints the stored date. Both backticks and $() work the same, but $() is easier to read and nest. The execution table shows step-by-step how the command runs, output is captured, and the variable is set. This helps beginners see how command substitution works in practice.