Challenge ·Hard

RHCSA Prep: Quoting and Escaping Special Characters in Shell Commands

Mister P
by  Mister P · on
Linux
Master Bash quoting forms — single quotes, double quotes, backslash escaping, and ANSI-C quoting — to correctly handle filenames and arguments containing spaces, glob characters, dollar signs, and newlines.

A junior admin left a directory full of files with problematic names and no documentation. Your job is to correctly handle those files using proper Bash quoting and escaping techniques.

Complete all four tasks below on rocky-01 as the laborant user, working inside ~/quote-test.

Hint 1 — Task 1 (creating files)

Think about what the shell does to unquoted spaces and $ before touch ever sees the arguments. Which quoting form stops all expansion?

Hint 2 — Task 1 (dollar sign gotcha)

Double quotes expand $list as a variable (almost certainly empty), giving you price.txt instead of price$list.txt. Single quotes suppress all expansion — use them for this filename.

Hint 3 — Task 2 (archiving special names)

When passing the filenames to tar, quote each one the same way you quoted them for touch. The shell still expands arguments before passing them to tar, so the same quoting rules apply.

Hint 4 — Task 3 (for loop with spaces)

Structure your loop like:

for f in 'name with space' 'other name'; do
  echo "$f"
done

Notice "$f" — without the double quotes the shell would split on spaces and treat each word as a separate argument.

Hint 5 — Task 4 (ANSI-C quoting)

ANSI-C quoting uses $'...' syntax. Inside it, \t becomes a real tab and \' becomes a real apostrophe. Example:

printf '%s\n' $'col1\tcol2'
printf '%s\n' $'it\'s ready'

Redirect the output to notes.txt using > and >> (or a single printf with two format strings).