Skip to main content

📘 Single Quotes '...'


🎯 What You Will Learn

  1. Understand what single quotes ('...') mean in Bash and how they control text interpretation.
  2. Learn how single quotes preserve literal values — preventing variable expansion and special character interpretation.
  3. Compare single quotes vs double quotes vs escape characters.
  4. See practical examples and outputs to understand how '...' behaves in real CLI commands.
  5. Apply single-quoted syntax safely in WordPress VPS Bash scripts (WP-CLI commands, paths, cron, and automation).
  6. Follow best practices and common mistake corrections for secure shell scripting.

1. **What Are Single Quotes **'...'

Single quotes in Bash define a literal string — meaning everything inside is taken exactly as written. Bash does not interpret variables, commands, or escape sequences within '...'. This makes single quotes ideal for:

  • Protecting text from shell expansion.
  • Writing file paths, commands, or strings containing special symbols ($, , ?, !, \, etc.).
  • Safely passing arguments to scripts or WP-CLI commands.

2. Why Single Quotes Matter

ReasonDescription
Literal ProtectionPrevents variables like $PATH or $HOME from expanding.
Safety in AutomationAvoids accidental command injection or interpretation in cron jobs.
Accuracy in Regex and JSONKeeps syntax stable when passing data with $, ", or \.
WordPress CLI CompatibilityEnsures safe database operations with special characters.

3. Where to Define or Use Single Quotes

ContextExample FilePurpose
Interactive CommandsCLI TerminalUse quotes to protect patterns or data directly.
Scripts~/scripts/wpbackup.shEmbed literal paths or SQL statements.
Automation Files/etc/cron.d/, .bashrcEnsure predictable execution in cron or startup environments.

4. File Structure Example (Tree View)

/home/
└── donnyariw/
├── scripts/
│ ├── wpbackup.sh
│ ├── wpsearchreplace.sh
│ └── literal-test.sh
├── .bashrc
└── .bash_aliases


5. Core Syntax

Single quote syntax is extremely simple:

'string'

Bash interprets the entire content literally — no substitutions or expansions.

6. Examples and Expected Outputs

Example 1 — Literal String Protection

echo 'Hello $USER, welcome!'

Output:

Hello $USER, welcome!

Explanation: The $USER variable is not expanded because it’s inside single quotes.

Example 2 — Compare with Double Quotes

echo "Hello $USER, welcome!"

Output:

Hello root, welcome!

Explanation: In double quotes, $USER is expanded to the actual username.

Example 3 — Protect Special Characters

echo 'The path is /home/$USER/public_html/*.php'

Output:

The path is /home/$USER/public_html/*.php

No filename expansion (*.php) occurs — everything is treated literally.

Example 4 — Combining Quotes Safely

echo 'You can use "double quotes" inside single quotes'

Output:

You can use "double quotes" inside single quotes

Explanation: Double quotes are literal text here — not syntax markers.

Example 5 — WordPress Example (WP-CLI)

wp search-replace 'http://oldsite.com' 'https://newsite.com' --allow-root

Output:

Success: Made 10 replacements.

Explanation: URLs are wrapped in '...' so Bash doesn’t interpret special characters like : or /.

Example 6 — Escaping Within Single Quotes

Single quotes cannot contain another single quote directly — Bash will interpret it as the end of the string. Incorrect:

echo 'Donny's VPS'

Output:

bash: syntax error near unexpected token `s'

Correct Way (Concatenate Parts):

echo 'Donny'\''s VPS'

Output:

Donny's VPS

Explanation: The string is broken: 'Donny' + \' + 's VPS'. This allows embedding an apostrophe or single quote.

7. WordPress VPS Use Cases

Use CaseExampleDescription
Search-Replace with Special Characterswp search-replace 'http://oldsite' 'https://newsite' --allow-rootPrevents $ or & from breaking the command.
SQL Queries in Bashmysql -e 'SELECT * FROM wp_users WHERE user_email="[email protected]";'Keeps SQL syntax literal.
Crontab Commands* * * * * root echo 'Running nightly backup' >> /root/logs/cron.logAvoids variable expansion in cron context.
Safe Regex Searchgrep 'define(.*DB_NAME' wp-config.phpPrevents Bash from interpreting parentheses.
WP-CLI Option Updatewp option update 'blogdescription' 'Fast & Secure VPS WordPress Hosting' --allow-rootKeeps ampersands literal and safe.

8. Practical Comparison Table

CaseInputOutputExpansion
Single Quotesecho '$USER'$USER❌ No
Double Quotesecho "$USER"root✅ Yes
Unquotedecho $USERroot✅ Yes
Escaped Characterecho \$USER$USER✅ Yes (via \)

9. Best Practices

  1. Use '...' for literal values — especially in automation and cron jobs.
  2. Avoid nesting single quotes directly — use concatenation or escape technique.
  3. Combine with double quotes when literal and expandable parts mix:
echo "The user is '$USER'"

  1. Always quote variables inside scripts — prevents word-splitting errors:
echo "$file"

  1. Use single quotes for regex, JSON, or SQL — Bash won’t interpret $, {}, or \.
  2. When in doubt — quote. It’s safer to overquote than to risk unintended expansion.

10. Common Mistakes and Corrections

MistakeResultCorrection
echo 'Donny's VPS'Syntax errorecho 'Donny'\''s VPS'
wp search-replace 'http://$OLD' 'https://$NEW'$OLD not expandedUse double quotes for variable: wp search-replace "$OLD" "$NEW"
Forgetting quotes around strings with spacesSplits into multiple argumentsAlways quote: 'My Blog Title'
Using single quotes in JSON within a scriptJSON invalidUse double quotes inside single quotes, or vice versa.

11. Quick Lab

Objective: Practice using single quotes to protect literal strings.

  1. Open terminal:
cd /home/donnyariw/scripts
nano literal-test.sh

  1. Add:
#!/bin/bash
echo 'This is a literal test.'
echo 'User variable: $USER'
echo 'Current path: $PWD'
echo 'Donny'\''s VPS is secure.'
wp option update 'blogdescription' 'Fast & Secure Hosting with $symbol' --allow-root

  1. Save and run:
bash literal-test.sh

Expected Output:

This is a literal test.
User variable: $USER
Current path: $PWD
Donny's VPS is secure.
Success: Updated 'blogdescription' option.

✅ Notice that $USER and $PWD remain unexpanded, demonstrating literal quoting.

12. Troubleshooting

IssueCauseFix
Syntax error near 'Nested single quotesUse 'text'\''text' method
Variable not expandingUsed single quotes by mistakeUse double quotes
Command not executing correctlyQuotes unbalancedCount matching pairs ('...')
Cron job misbehavingVariables not loaded in cronUse absolute paths and single quotes

13. Glossary

TermDefinition
QuotingTelling Bash how to interpret text or special characters.
Literal StringText that Bash does not modify or expand.
ExpansionBash replacing variables or commands ($VAR, $(cmd)).
ConcatenationJoining strings together, e.g., 'Donny'\''s VPS'.
EscapeA method to cancel special meaning using \.

14. Mini-Quiz

  1. What happens to $USER when enclosed in single quotes?
  2. How do you include a literal apostrophe inside single quotes?
  3. Which quoting method should you use for strings containing both $VAR and spaces?
  4. Why is '...' safer than unquoted text in cron jobs?
  5. How do single quotes differ from double quotes in Bash?

15. Summary

  • Single quotes '...' create literal strings — Bash does not expand variables or interpret special symbols.
  • Ideal for safe automation, regex, SQL, and WP-CLI arguments.
  • To include an apostrophe, use concatenation: 'Donny'\''s VPS'.
  • Always quote your arguments to prevent unwanted expansion, especially in scripts and cron jobs.
  • Combine single quotes with double quotes strategically for flexibility.

Would you like me to continue next with the next quoting topic — Double Quotes ****"...", explaining variable expansion, command substitution, and mixed literal + dynamic text behavior?