Skip to main content

📘 Double Quotes "..."


🎯 What You Will Learn

  1. Understand how double quotes ("...") behave in Bash and how they differ from single quotes.
  2. Learn how variable, command, and escape sequence expansion work inside double quotes.
  3. See practical examples showing differences between literal ('...') and expandable ("...") text.
  4. Apply "..." safely in WordPress VPS Bash scripts, especially for WP-CLI, file paths, and cron jobs.
  5. 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

ReasonDescription
Variable Expansion$USER, $PWD, or custom variables expand correctly.
Command Output InclusionInsert results of $(command) inline.
Controlled InterpretationKeeps words together, avoids unwanted splitting.
Safe String HandlingIdeal for dynamic paths, messages, or arguments.
WP-CLI AutomationMixes literal and variable data in scripts safely.

3. Where to Use Double Quotes

ContextExamplePurpose
Interactive Commandsecho "Hello $USER"Expands current user dynamically.
Scriptswp 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 $USER or $PWD are 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

TaskExampleExplanation
Plugin Updatewp plugin update --path="/home/$USER/public_html" --allow-rootExpands $USER dynamically.
Database Exportwp db export "/root/wpbackup/${site}-$(date +%F).sql"Combines variable and command output.
Log Entryecho "[$(date)] Backup done for $site" >> /root/logs/backup.logAdds timestamp dynamically.
Search & Replacewp search-replace "$OLD_URL" "$NEW_URL" --allow-rootExpands both URLs safely.
Conditional Debugecho "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 CaseLiteral dataDynamic data

9. Best Practices

  1. 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'"

  1. Escape inner quotes with \":
echo "He said, \"Hello!\""

  1. For JSON or SQL, mix '...' and "..." carefully:
echo '{"site":"'"$site"'","env":"prod"}'

  1. 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

MistakeProblemCorrection
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.sqlFails 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.

  1. Create file:
nano ~/scripts/quote-demo.sh

  1. 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\""

  1. 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

IssueCauseFix
Variables not expandingUsed '...' instead of "..."Replace with double quotes
Syntax error with nested quotesMixed unescaped quotesEscape inner \" or use 'text'"$var"'text'
Incorrect word splittingMissing quotes around $varAlways quote variable references
Cron job not expanding $VAREnvironment missingDefine variables inside command or use absolute paths

13. Glossary

TermDefinition
ExpansionBash replaces variables or executes commands inside quotes.
Command SubstitutionEmbedding command output into another command using $(...).
Escape SequenceBackslash (\) used to insert literal special characters.
Word SplittingWhen unquoted text is broken into multiple arguments.
ConcatenationJoining text and variables together in one quoted string.

14. Mini-Quiz

  1. What happens to $USER inside "..."?
  2. How do you include a literal " inside double quotes?
  3. Why is "${var}" safer than $var in scripts?
  4. Which quotes should you use when combining variables and fixed words?
  5. 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.