Pipes and text
Practice shell quoting and output
Use a Bash exercise to compare quoted and unquoted output, preserve literal characters, and write a small report without losing spaces or newlines.
We’ll use echo to see what the shell does to text before a command receives it. Then we’ll write a small report with predictable output.
Use Bash for this exercise on Linux or macOS. If your terminal uses zsh or Fish, run bash first. The echo reference covers its flags and the differences between shells.
Make a practice folder
Create a temporary folder so the filename patterns below have predictable matches:
practice_dir=$(mktemp -d)
cd "$practice_dir"
touch apple.txt banana.txt
Only work inside this folder for the exercise.
Compare quotes
Assign a message containing extra spaces and an asterisk:
message='Ready to ship *'
Predict the output of each command, then run them:
echo $message
echo "$message"
echo '$message'
You should see:
Ready to ship apple.txt banana.txt
Ready to ship *
$message
Without quotes, Bash splits the expanded variable into words. It then replaces * with matching filenames. echo joins those arguments with single spaces.
Double quotes expand the variable but keep its spaces and literal asterisk. Single quotes prevent variable expansion, so we print $message itself.
Now create another file and run the unquoted command again:
touch cherry.txt
echo $message
The output now includes cherry.txt. The variable didn’t change; the shell expanded its asterisk using the current folder’s contents.
Check where the newline comes from
Print a short progress message using two commands:
echo -n 'Packing: '
echo 'done'
The result is one line, Packing: done. In Bash, -n omits the first command’s final newline. The second command supplies one.
For scripts that need predictable output, use a fixed printf format. Here, the format adds the newline, and the variable supplies the text:
printf '%s\n' "$message"
Write a report
Create a report with the message and a literal pattern. These commands write only to the temporary folder we created:
printf '%s\n' "$message" > report.txt
printf '%s\n' 'Pattern: *.txt' >> report.txt
cat report.txt
The contents should be:
Ready to ship *
Pattern: *.txt
> creates or replaces the file. >> appends the second line. Each fixed format preserves the text and adds exactly one newline.
Try appending the same line again, then print the report. You should now have three lines. Rerunning an append command adds the same output again.
Try it yourself
Set message to -n and print it as a complete line. Use the fixed printf format from above. echo "$message" treats -n as an option in Bash even though the variable is quoted.
Then make a two-line report containing a literal $HOME on the first line and your home folder’s path on the second. Use single quotes for the literal text and double quotes around the variable expansion.
Check the file with cat. When you’re finished, run cd to return to your home folder. If you entered Bash only for this exercise, use exit to return to your usual shell.
Lesson completed