📘 Single Quotes '...'
🎯 What You Will Learn
- Understand what single quotes (
'...') mean in Bash and how they control text interpretation. - Learn how single quotes preserve literal values — preventing variable expansion and special character interpretation.
- Compare single quotes vs double quotes vs escape characters.
- See practical examples and outputs to understand how
'...'behaves in real CLI commands. - Apply single-quoted syntax safely in WordPress VPS Bash scripts (WP-CLI commands, paths, cron, and automation).
- 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
| Reason | Description |
| Literal Protection | Prevents variables like $PATH or $HOME from expanding. |
| Safety in Automation | Avoids accidental command injection or interpretation in cron jobs. |
| Accuracy in Regex and JSON | Keeps syntax stable when passing data with $, ", or \. |
| WordPress CLI Compatibility | Ensures safe database operations with special characters. |
3. Where to Define or Use Single Quotes
| Context | Example File | Purpose |
| Interactive Commands | CLI Terminal | Use quotes to protect patterns or data directly. |
| Scripts | ~/scripts/wpbackup.sh | Embed literal paths or SQL statements. |
| Automation Files | /etc/cron.d/, .bashrc | Ensure 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 Case | Example | Description |
| Search-Replace with Special Characters | wp search-replace 'http://oldsite' 'https://newsite' --allow-root | Prevents $ or & from breaking the command. |
| SQL Queries in Bash | mysql -e 'SELECT * FROM wp_users WHERE user_email="[email protected]";' | Keeps SQL syntax literal. |
| Crontab Commands | * * * * * root echo 'Running nightly backup' >> /root/logs/cron.log | Avoids variable expansion in cron context. |
| Safe Regex Search | grep 'define(.*DB_NAME' wp-config.php | Prevents Bash from interpreting parentheses. |
| WP-CLI Option Update | wp option update 'blogdescription' 'Fast & Secure VPS WordPress Hosting' --allow-root | Keeps ampersands literal and safe. |
8. Practical Comparison Table
| Case | Input | Output | Expansion |
| Single Quotes | echo '$USER' | $USER | ❌ No |
| Double Quotes | echo "$USER" | root | ✅ Yes |
| Unquoted | echo $USER | root | ✅ Yes |
| Escaped Character | echo \$USER | $USER | ✅ Yes (via \) |
9. Best Practices
- Use
'...'for literal values — especially in automation and cron jobs. - Avoid nesting single quotes directly — use concatenation or escape technique.
- Combine with double quotes when literal and expandable parts mix:
echo "The user is '$USER'"
- Always quote variables inside scripts — prevents word-splitting errors:
echo "$file"
- Use single quotes for regex, JSON, or SQL — Bash won’t interpret
$,{}, or\. - When in doubt — quote. It’s safer to overquote than to risk unintended expansion.
10. Common Mistakes and Corrections
| Mistake | Result | Correction |
echo 'Donny's VPS' | Syntax error | echo 'Donny'\''s VPS' |
wp search-replace 'http://$OLD' 'https://$NEW' | $OLD not expanded | Use double quotes for variable: wp search-replace "$OLD" "$NEW" |
| Forgetting quotes around strings with spaces | Splits into multiple arguments | Always quote: 'My Blog Title' |
| Using single quotes in JSON within a script | JSON invalid | Use double quotes inside single quotes, or vice versa. |
11. Quick Lab
Objective: Practice using single quotes to protect literal strings.
- Open terminal:
cd /home/donnyariw/scripts
nano literal-test.sh
- 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
- 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
| Issue | Cause | Fix |
Syntax error near ' | Nested single quotes | Use 'text'\''text' method |
| Variable not expanding | Used single quotes by mistake | Use double quotes |
| Command not executing correctly | Quotes unbalanced | Count matching pairs ('...') |
| Cron job misbehaving | Variables not loaded in cron | Use absolute paths and single quotes |
13. Glossary
| Term | Definition |
| Quoting | Telling Bash how to interpret text or special characters. |
| Literal String | Text that Bash does not modify or expand. |
| Expansion | Bash replacing variables or commands ($VAR, $(cmd)). |
| Concatenation | Joining strings together, e.g., 'Donny'\''s VPS'. |
| Escape | A method to cancel special meaning using \. |
14. Mini-Quiz
- What happens to
$USERwhen enclosed in single quotes? - How do you include a literal apostrophe inside single quotes?
- Which quoting method should you use for strings containing both
$VARand spaces? - Why is
'...'safer than unquoted text in cron jobs? - 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?