Introduction
In this lab, you will learn a powerful trick called Command Substitution.
Imagine you are writing an email and want to include today's date. You just want the computer to insert "today's date" automatically.
In Linux, we use $(). It tells the computer: "Run this command first, then put the result right here."
This is one of the shell's most useful composition features. Instead of manually copying output from one command and pasting it into another, the shell can do that substitution for you automatically.
In this lab, you will:
- Learn how to format the
datecommand. - Create a folder that is automatically named with today's date (e.g.,
2025-12-23).
Format the Date
First, let's get the date in the format we want.
If you just run date, it gives you a messy string.
That is a good reminder that many commands support formatted output. Linux tools often let you choose between a human-friendly display and a machine-friendly format depending on what you need next.
To create a clean folder name like 2025-12-23, use +%F:
date +%F
Output (example):
2025-12-23
Dynamic Folder Creation
Now for the magic trick.
We want to make a directory using the date, but without typing it manually.
We put the date command inside the mkdir command using $(). The computer sees $() and does that part first.
Run this:
mkdir $(date +%F)
What happened:
- The shell saw
$(date +%F). - It ran
date +%Fsecretly and got the result (e.g.,2025-12-23). - It replaced the
$()part with that result. - It ran
mkdir 2025-12-23.
This shows that the shell builds the final command in stages. Command substitution happens before the command is finally executed, which is why the result can be used as a normal argument.
Verify Folder
Let's check if it worked.
Run ls:
ls -l
You should see a new folder named with today's date.
If you run this script tomorrow, it will create a new folder for tomorrow. This is how automated backups work!
Summary
In this lab, you learned about Command Substitution—the art of using the output of one command as the input for another.
- The Trick:
$(command) - The Process: Linux runs the inner command first, then swaps it with the result.
- The Result: You can create dynamic commands that change based on time, user, or other conditions without typing them manually.
Try running echo "Today is $(date)" to see it in action again!



