Bash syntax: quoting
Keep a filename together
Suppose a file is named daily notes.txt. Bash reads spaces and characters such as *, $, ;, and | as syntax in some places. Without quotes, the space can divide the filename into two arguments. Quoting tells Bash which characters to keep together and which operators to treat as ordinary text. The quote marks are removed before the command receives the arguments.
Single quotes keep every enclosed character literal. A single quote cannot appear directly inside a single-quoted string. Double quotes keep spaces and most special characters together while still allowing variable and command substitution. Outside quotes, a backslash quotes the next character. A backslash followed by a newline continues the command on the next input line.
value='two words'
printf '<%s>\n' "$value"
printf '%s\n' '$HOME' "$HOME"
The expansion of $value stays one argument, two words, despite the space, so the first printf prints it between angle brackets. The next command prints the literal text $HOME, then the value of the HOME variable. Single quotes suppress expansion; double quotes allow the variable to expand while keeping its result as one argument.
After a variable or command substitution, an unquoted value may undergo word splitting and pathname expansion. Word splitting can turn a value with spaces into several arguments. Pathname expansion can turn * into matching filenames. Quoting "$filename" prevents those steps:
printf '%s\n' "$filename"
grep -F -- "$needle" "$filename"
Here -F makes grep treat the search string literally; -- ends option parsing on implementations that support it. Those safeguards work at different layers. Shell quotes control how Bash builds arguments; the receiving program interprets those arguments according to its own options and syntax.
Quoting a wildcard makes it literal: printf '%s\n' "*.log" prints *.log without expanding it. To match files in a directory whose name may contain spaces, quote the directory and leave the wildcard active: printf '%s\n' "$directory"/*.log. If nothing matches, Bash normally leaves the pattern unchanged; nullglob and failglob change that result.
Quote marks inside a variable's value are ordinary characters; expansion does not make Bash parse them again. For a list of arguments, use an array and expand it as "${items[@]}" so each element stays separate. Bash also supports ANSI-C quoting such as $'line\nnext', which interprets backslash escapes and differs from ordinary single quotes. Treat quoted substitutions as the default, and leave a pattern unquoted only when you intend filename matching.




