BASH / CHAPTER 01

Words, quotes, and arguments

Beginner

Keep data intact when Bash builds a command line.

By the end of this chapter

  • Distinguish single and double quotes
  • Preserve an expanded value as one argument
  • Use printf with a fixed format

The idea

These lessons use Bash 5, not a generic POSIX shell. A command receives arguments after Bash parses words and performs expansions. Spaces in the source separate words unless quoting protects them. Single quotes preserve literal characters; double quotes allow parameter expansion while preventing ordinary word splitting and filename expansion of the result. Quoting is therefore part of preserving a value’s structure.

Store one piece of data in a variable and expand it as a quoted argument. Prefer a fixed printf format with data supplied separately, so percent signs inside input are not interpreted as formatting instructions. Do not build a command as one string and expect quoting inside that string to be reparsed safely. Arrays and explicit arguments provide a clearer foundation for later automation.

Bash example
#!/usr/bin/env bash
name="Ada Lovelace"
printf 'Hello, %s\n' "$name"
printf '%s\n' '$name'
Result
Hello, Ada Lovelace
$name

Double quotes expand name as one argument. Single quotes preserve the literal dollar sign and variable name in the second command.

Try it yourself

Print an arbitrary label containing spaces, asterisks, and percent signs without changing its content.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

Which expansion passes a variable containing spaces as one argument?