0
0
Bash Scriptingscripting~15 mins

Backticks and $() for command substitution in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Backticks and $() for Command Substitution in Bash
📖 Scenario: You are writing a simple bash script to display information about files in a directory. You want to capture the output of commands and use it inside your script.
🎯 Goal: Learn how to use backticks `command` and $(command) to run commands inside your script and store their output in variables.
📋 What You'll Learn
Create a variable with the output of a command using backticks
Create a variable with the output of a command using $() syntax
Use these variables in your script
Print the results to the terminal
💡 Why This Matters
🌍 Real World
Command substitution is used in scripts to capture the output of commands and use it for decisions, calculations, or messages.
💼 Career
Knowing how to capture command output is essential for writing effective bash scripts in system administration, automation, and DevOps roles.
Progress0 / 4 steps
1
Create a variable using backticks for command substitution
Create a variable called file_count_backticks that stores the number of files in the current directory using the command ls | wc -l inside backticks.
Bash Scripting
Need a hint?

Use backticks ` around the command ls | wc -l to capture its output.

2
Create a variable using $() for command substitution
Create a variable called file_count_dollar that stores the number of files in the current directory using the command ls | wc -l inside $( ).
Bash Scripting
Need a hint?

Use $(command) syntax to capture the output of ls | wc -l.

3
Use the variables in a message
Write a line that creates a variable called message with the text: "There are X files (backticks) and Y files ($()) in the directory." where X is the value of file_count_backticks and Y is the value of file_count_dollar. Use double quotes and variable substitution.
Bash Scripting
Need a hint?

Use double quotes and $variable to insert variable values inside the string.

4
Print the message to the terminal
Write a line to print the variable message to the terminal using echo.
Bash Scripting
Need a hint?

Use echo "$message" to print the message with variable values.