Przejdลบ do treล›ci

๐Ÿ’ฌ Quoting

Proper quoting prevents unexpected word splitting, glob expansion, and variable substitution.

๐Ÿงญ Three Types of Quotes

Quote Type Prevents Expansion Of... Allows Substitution?
Double (") Whitespace splitting, globbing Yes
Single (') Everything No
Backtick (`) Everything except backticks Yes (deprecated)

๐Ÿงช Examples

Double Quotes ("...")

Preserve whitespace and prevent globbing, but expand variables:

1
2
3
4
name="John Doe"
echo "Hello $name"        # Hello John Doe
files="*.txt"
echo "$files"             # *.txt (not expanded)

Single Quotes ('...')

Preserve everything literally:

1
echo '$HOME is not expanded'  # $HOME is not expanded

Mixed Quotes

Sometimes needed for complex expressions:

1
echo "Today is $(date '+%Y-%m-%d')"


๐Ÿง  Special Cases

Avoid unquoted variables:

1
2
bad_usage=$undefined_var      # May cause errors if undefined
good_usage="${undefined_var}" # Safe even if empty

Escape special characters manually:

1
echo \$HOME                   # Prints literal $HOME

Protect filenames with spaces:

1
mv "My Document.txt" doc.txt


๐Ÿงพ Summary

  • Use double quotes to protect strings containing spaces or variables.
  • Use single quotes for literal strings.
  • Escape individual characters with backslash \.
  • Never leave variables unquoted unless intentional.

๐Ÿ‘‰ Continue to: Globbing