Exercise
Quoting Exercise
Overview
To stop Bash from expanding special characters or splitting arguments on spaces, you must protect your text with single quotes, double quotes, or backslashes.
In this hands-on exercise you will print a literal asterisk in three ways, assign and expand a variable, compare single and double quotes, and group words into arguments with quotes and backslashes.
- Target host
servera
-
Print a literal asterisk
Connect to
serveraand work from/tmp. First observe the unquoted result, then protect the asterisk with single quotes, double quotes, and a backslash so it reachesecholiterally.ssh student@servera cd /tmp echo * echo '*' echo "*" echo \* -
Assign a value to a variable
Assign a value with
name=value(no spaces around=). Read the value back with$linux.linux=awesome echo See the value with: $linux -
Use single and double quotes
Run both commands and compare their outputs.
echo "Double quotes allow variable expansion: $linux" echo 'Single quotes treat everything literally: $linux' -
Include spaces in arguments
First observe how unquoted spaces split words into separate arguments. Then group words into directory names with single quotes and backslashes. Verify each result with
ls -l, then clean up and unset the variable.mkdir My Files ls -l mkdir 'Linux Files' ls -l mkdir This\ Is\ One\ Argument ls -l rmdir My Files 'Linux Files' 'This Is One Argument' unset linux