📘 Double Quotes "..."
🎯 What You Will Learn
- Understand how double quotes (
"...") behave in Bash and how they differ from single quotes. - Learn how variable, command, and escape sequence expansion work inside double quotes.
- See practical examples showing differences between literal (
'...') and expandable ("...") text. - Apply
"..."safely in WordPress VPS Bash scripts, especially for WP-CLI, file paths, and cron jobs. - Follow best practices to avoid expansion-related bugs, security risks, or output formatting issues.
1. **What Are Double Quotes **"..."
In Bash, double quotes allow controlled expansion.
Text inside "..." is preserved as a single unit, but Bash still evaluates:
- Variables →
$VAR - Command substitution →
$(command) - Arithmetic expansion →
$((expression)) - Escape sequences →
\",\$,\n, etc. Thus,"..."provides a balance between safety and flexibility — it prevents word splitting but allows expansion.
2. Why Double Quotes Matter
| Reason | Description |
| Variable Expansion | $USER, $PWD, or custom variables expand correctly. |
| Command Output Inclusion | Insert results of $(command) inline. |
| Controlled Interpretation | Keeps words together, avoids unwanted splitting. |
| Safe String Handling | Ideal for dynamic paths, messages, or arguments. |
| WP-CLI Automation | Mixes literal and variable data in scripts safely. |
3. Where to Use Double Quotes
| Context | Example | Purpose |
| Interactive Commands | echo "Hello $USER" | Expands current user dynamically. |
| Scripts | wp search-replace "$OLD" "$NEW" | Replaces variables with real values. |
| Automation (Cron) | bash -c "echo \"$DATE\" >> /root/logs/job.log" | Keeps variables intact in cron context. |
4. File Location (Tree View Example)
/home/
└── donnyariw/
├── scripts/
│ ├── wpupdate.sh
│ ├── wpbackup.sh
│ └── quote-demo.sh
├── .bashrc
└── .bash_aliases
5. Core Syntax
"string"
Inside double quotes:
- Variables like
$USERor$PWDare expanded. - Command substitution
$(...)is executed. - Escape sequences
\n,\",\$are interpreted.
6. Examples and Expected Outputs
Example 1 — Variable Expansion
echo "Hello $USER, welcome to $HOSTNAME"
Output:
Hello root, welcome to GC-SG-M16GB
Explanation:
Bash expands both $USER and $HOSTNAME.
Example 2 — Compare with Single Quotes
echo 'Hello $USER'
Output:
Hello $USER
Single quotes keep text literal, double quotes allow expansion.
Example 3 — Command Substitution
echo "Current directory is $(pwd)"
Output:
Current directory is /root
Explanation:
$(pwd) runs first, then its output is inserted into the string.
Example 4 — Preserve Spaces
name="Donny Ari"
echo "Welcome $name"
Output:
Welcome Donny Ari
Without quotes, Bash would treat “Donny” and “Ari” as separate arguments.
Example 5 — Escaping Characters Inside Double Quotes
echo "He said, \"WordPress is powerful!\""
Output:
He said, "WordPress is powerful!"
Explanation:
Use backslash (\) to include literal double quotes inside "...".
Example 6 — WordPress VPS Example
site="dev-wpstrategist"
wp db export "/root/wpbackup/${site}-$(date +%F).sql" --allow-root
Output:
Success: Exported to /root/wpbackup/dev-wpstrategist-2025-10-13.sql
Explanation:
Variables ($site, $(date)) expand to generate a dynamic file name safely.
Example 7 — Combining Literal and Dynamic Text
echo "The path is '/home/$USER/public_html'"
Output:
The path is '/home/root/public_html'
Quotes can be nested: single quotes stay literal inside double quotes.
7. Use in WordPress Automation
| Task | Example | Explanation |
| Plugin Update | wp plugin update --path="/home/$USER/public_html" --allow-root | Expands $USER dynamically. |
| Database Export | wp db export "/root/wpbackup/${site}-$(date +%F).sql" | Combines variable and command output. |
| Log Entry | echo "[$(date)] Backup done for $site" >> /root/logs/backup.log | Adds timestamp dynamically. |
| Search & Replace | wp search-replace "$OLD_URL" "$NEW_URL" --allow-root | Expands both URLs safely. |
| Conditional Debug | echo "Debug mode: ${DEBUG_MODE:-off}" | Shows default value if variable unset. |
8. Comparison: Single vs Double Quotes
| Feature | 'Single Quotes' | "Double Quotes" |
| Variable Expansion | ❌ No | ✅ Yes |
| Command Substitution | ❌ No | ✅ Yes |
| Escape Sequences | ❌ No | ✅ Yes |
| Literal Output | ✅ Always | ⚠️ Partial |
| Use Case | Literal data | Dynamic data |
9. Best Practices
- Always quote variables to prevent word-splitting:
echo "$file"
Avoids issues when filenames have spaces. 2. Prefer double quotes when combining literals and variables:
echo "User is '$USER'"
- Escape inner quotes with
\":
echo "He said, \"Hello!\""
- For JSON or SQL, mix
'...'and"..."carefully:
echo '{"site":"'"$site"'","env":"prod"}'
- In cron jobs, wrap commands in double quotes and escape internal
$:
* * * * * root bash -c "echo \"Cron run at $(date)\" >> /root/logs/cron.log"
10. Common Mistakes & Fixes
| Mistake | Problem | Correction |
echo "It's fine" | Works accidentally, but can confuse parser in nested quotes. | Use 'It\'s fine' or mix quotes safely. |
wp db export /root/wpbackup/$site.sql | Fails if $site has spaces. | Quote: "${site}.sql". |
echo "Backup path: $backup_dir$site.sql" | Misses separator. | Use "${backup_dir}/${site}.sql". |
echo "WordPress path is $path/public_html" | Works, but ambiguous if $path ends with /. | "${path}/public_html". |
11. Quick Lab
Objective: Demonstrate variable expansion and command substitution inside double quotes.
- Create file:
nano ~/scripts/quote-demo.sh
- Add content:
#!/bin/bash
user=$USER
site="dev-wpstrategist"
today=$(date +%F)
echo "User: $user"
echo "Backup file: /root/wpbackup/${site}-${today}.sql"
echo "Current directory: $(pwd)"
echo "System uptime: $(uptime -p)"
echo "Nested quotes example: \"Hello $USER\""
- Save and run:
bash ~/scripts/quote-demo.sh
Expected Output:
User: root
Backup file: /root/wpbackup/dev-wpstrategist-2025-10-13.sql
Current directory: /home/donnyariw/scripts
System uptime: up 2 hours, 31 minutes
Nested quotes example: "Hello root"
✅ Demonstrates expansion, substitution, and quoting inside one script.
12. Troubleshooting
| Issue | Cause | Fix |
| Variables not expanding | Used '...' instead of "..." | Replace with double quotes |
| Syntax error with nested quotes | Mixed unescaped quotes | Escape inner \" or use 'text'"$var"'text' |
| Incorrect word splitting | Missing quotes around $var | Always quote variable references |
Cron job not expanding $VAR | Environment missing | Define variables inside command or use absolute paths |
13. Glossary
| Term | Definition |
| Expansion | Bash replaces variables or executes commands inside quotes. |
| Command Substitution | Embedding command output into another command using $(...). |
| Escape Sequence | Backslash (\) used to insert literal special characters. |
| Word Splitting | When unquoted text is broken into multiple arguments. |
| Concatenation | Joining text and variables together in one quoted string. |
14. Mini-Quiz
- What happens to
$USERinside"..."? - How do you include a literal
"inside double quotes? - Why is
"${var}"safer than$varin scripts? - Which quotes should you use when combining variables and fixed words?
- What’s the output of
echo "Today is $(date +%A)"?
15. Summary
- Double quotes let you combine literal text with expanded variables or commands.
- Unlike single quotes, they allow variable, command, and escape expansion.
- Use
"..."for dynamic text, file paths, and automation scripts where variable substitution is needed. - Always quote variables to prevent unexpected splitting or expansion errors.
- Escape internal quotes with
\", and mix single quotes when needed for clarity.