Command Syntax and Quoting

Turn a command line into predictable arguments, then save it as a script.

How Bash reads a command

printf '%s\n' 'weekly report.txt'
Part Role
printf Command name
'%s\n' Format argument
'weekly report.txt' One data argument containing a space

Quotes guide parsing; they are removed before the command receives its arguments.

Protect data from expansion

label='Review $5 | *.txt'
printf '%s\n' "$label"

Expected: Review $5 | *.txt

Single quotes preserve literal text. Double quotes expand $label while keeping its value together. Expansion of that variable does not turn its | into a pipeline operator.

Predict the arguments

report='weekly report.txt'
printf '<%s>\n' "$report"

Expected: <weekly report.txt>

With an unquoted expansion, word splitting and filename expansion can change the arguments. Use a fixed format and quoted data: printf '%s\n' "$report".

Practice: Explain why printf "$report" gives the data a different role.

Save a script

cat > review.sh <<'SCRIPT'
#!/bin/bash
printf '%s\n' 'Review $5 | *.txt'
SCRIPT
bash -n review.sh
bash review.sh

The quoted here-document delimiter preserves the script text. bash -n checks syntax; it does not prove the script produces the correct result.

Interpreter invocation and direct execution

Invocation Requirements
bash review.sh Bash reads the script; executable permission is unnecessary
./review.sh Executable permission, usable interpreter line, and filesystem permission to execute
chmod u+x review.sh
./review.sh

./ selects the file in the current directory. A bare review.sh depends on command lookup.

Verify the host-check script

Use materials/05/host_check.sh from the lesson.

  1. Inspect the source and run bash -n.
  2. Run through Bash and by explicit path.
  3. Compare the results on your VM.
  4. Explain which fields depend on the machine.

Keep host details in your own workspace. The script observes the host; it does not configure it.