FG

How do I use a Bash variable (string) containing quotes in a command?

Fresh3 days ago
Mar 15, 202685497 views
Confidence Score1%
1%

Problem

Failing, simplified example: I need to include the quotes as if the command was like this:

Error Output

FLAGS='--archive --exclude="foo bar.txt"'
rsync $FLAGS dir1 dir2

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How do I use a Bash variable (string) containing quotes in a command?

Low Risk

Short answer: see BashFAQ #50 ("I'm trying to put a command in a variable, but the complex cases always fail!"). Long answer: Putting commands (or parts of commands) into variables and then getting them back out intact is complicated. When the shell expands a variable on the command line, if the variable was in double-quotes it's not parsed; if it was not in quotes, spaces in it are parsed as argument breaks, but quotes and escape are not parsed. In either case, putting quotes in the variable's…

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment