Expanding a variable containing an equals sign and curly braces
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz
.
However, upon trying to expand it within the rsync
command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes
becomes rsync '--exclude=foo,bar,baz'
, rather than the expected rsync --exclude=foo,bar,baz
. Doing some testing, I found that any string containing =
, , or
will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?
bash variable
 |Â
show 3 more comments
up vote
1
down vote
favorite
I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz
.
However, upon trying to expand it within the rsync
command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes
becomes rsync '--exclude=foo,bar,baz'
, rather than the expected rsync --exclude=foo,bar,baz
. Doing some testing, I found that any string containing =
, , or
will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?
bash variable
Do you really want it to expand to--exclude=foo,bar,baz
or are you trying to have brace expansion expand it to:--exclude=foo --exclude=bar --exclude=baz
?
â Jesse_b
Jul 4 at 14:35
The former is the goal.
â ollien
Jul 4 at 14:35
I also think that isn't the right syntax. Should be--exclude foo
. I don't think rsync takes braces either though.
â Jesse_b
Jul 4 at 14:36
Using the equals sign is the correct syntax.
â ollien
Jul 4 at 14:36
From the man page,--exclude=PATTERN exclude files matching PATTERN
â ollien
Jul 4 at 14:38
 |Â
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz
.
However, upon trying to expand it within the rsync
command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes
becomes rsync '--exclude=foo,bar,baz'
, rather than the expected rsync --exclude=foo,bar,baz
. Doing some testing, I found that any string containing =
, , or
will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?
bash variable
I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz
.
However, upon trying to expand it within the rsync
command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes
becomes rsync '--exclude=foo,bar,baz'
, rather than the expected rsync --exclude=foo,bar,baz
. Doing some testing, I found that any string containing =
, , or
will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?
bash variable
asked Jul 4 at 14:23
ollien
1045
1045
Do you really want it to expand to--exclude=foo,bar,baz
or are you trying to have brace expansion expand it to:--exclude=foo --exclude=bar --exclude=baz
?
â Jesse_b
Jul 4 at 14:35
The former is the goal.
â ollien
Jul 4 at 14:35
I also think that isn't the right syntax. Should be--exclude foo
. I don't think rsync takes braces either though.
â Jesse_b
Jul 4 at 14:36
Using the equals sign is the correct syntax.
â ollien
Jul 4 at 14:36
From the man page,--exclude=PATTERN exclude files matching PATTERN
â ollien
Jul 4 at 14:38
 |Â
show 3 more comments
Do you really want it to expand to--exclude=foo,bar,baz
or are you trying to have brace expansion expand it to:--exclude=foo --exclude=bar --exclude=baz
?
â Jesse_b
Jul 4 at 14:35
The former is the goal.
â ollien
Jul 4 at 14:35
I also think that isn't the right syntax. Should be--exclude foo
. I don't think rsync takes braces either though.
â Jesse_b
Jul 4 at 14:36
Using the equals sign is the correct syntax.
â ollien
Jul 4 at 14:36
From the man page,--exclude=PATTERN exclude files matching PATTERN
â ollien
Jul 4 at 14:38
Do you really want it to expand to
--exclude=foo,bar,baz
or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz
?â Jesse_b
Jul 4 at 14:35
Do you really want it to expand to
--exclude=foo,bar,baz
or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz
?â Jesse_b
Jul 4 at 14:35
The former is the goal.
â ollien
Jul 4 at 14:35
The former is the goal.
â ollien
Jul 4 at 14:35
I also think that isn't the right syntax. Should be
--exclude foo
. I don't think rsync takes braces either though.â Jesse_b
Jul 4 at 14:36
I also think that isn't the right syntax. Should be
--exclude foo
. I don't think rsync takes braces either though.â Jesse_b
Jul 4 at 14:36
Using the equals sign is the correct syntax.
â ollien
Jul 4 at 14:36
Using the equals sign is the correct syntax.
â ollien
Jul 4 at 14:36
From the man page,
--exclude=PATTERN exclude files matching PATTERN
â ollien
Jul 4 at 14:38
From the man page,
--exclude=PATTERN exclude files matching PATTERN
â ollien
Jul 4 at 14:38
 |Â
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).
I think the following should work for you:
excludes=( $(--exclude=foo,bar,baz) )
rsync "$excludes[@]"
This does not work. This expands torsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
â ollien
Jul 4 at 15:03
@ollien: That is what you need.--exclude=foo,bar,baz
would be a syntax error to rsync
â Jesse_b
Jul 4 at 15:07
rsync does not recognize the flag when it is prepended by a single quote.
â ollien
Jul 4 at 15:09
The single quote doesn't actually go to rsync, it will be consumed by the shell
â Jesse_b
Jul 4 at 15:10
1
You definitely do not need theecho
in there.excl=( --exclude=a,b,c )
works inbash
.
â Kusalananda
Jul 8 at 12:24
 |Â
show 1 more comment
up vote
1
down vote
From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.
If you mean the xtrace
output (set -x
), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.
That doesn't mean the quotes are part of the string, though.
Compare:
$ echo abc def ghi
+ echo abc 'def ghi'
abc def ghi
$ echo abc 'def ghi'
+ echo abc ''''def ghi''''
abc 'def ghi'
Though as far as I can see, it doesn't bother to quote strings containing the equal sign.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).
I think the following should work for you:
excludes=( $(--exclude=foo,bar,baz) )
rsync "$excludes[@]"
This does not work. This expands torsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
â ollien
Jul 4 at 15:03
@ollien: That is what you need.--exclude=foo,bar,baz
would be a syntax error to rsync
â Jesse_b
Jul 4 at 15:07
rsync does not recognize the flag when it is prepended by a single quote.
â ollien
Jul 4 at 15:09
The single quote doesn't actually go to rsync, it will be consumed by the shell
â Jesse_b
Jul 4 at 15:10
1
You definitely do not need theecho
in there.excl=( --exclude=a,b,c )
works inbash
.
â Kusalananda
Jul 8 at 12:24
 |Â
show 1 more comment
up vote
2
down vote
accepted
Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).
I think the following should work for you:
excludes=( $(--exclude=foo,bar,baz) )
rsync "$excludes[@]"
This does not work. This expands torsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
â ollien
Jul 4 at 15:03
@ollien: That is what you need.--exclude=foo,bar,baz
would be a syntax error to rsync
â Jesse_b
Jul 4 at 15:07
rsync does not recognize the flag when it is prepended by a single quote.
â ollien
Jul 4 at 15:09
The single quote doesn't actually go to rsync, it will be consumed by the shell
â Jesse_b
Jul 4 at 15:10
1
You definitely do not need theecho
in there.excl=( --exclude=a,b,c )
works inbash
.
â Kusalananda
Jul 8 at 12:24
 |Â
show 1 more comment
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).
I think the following should work for you:
excludes=( $(--exclude=foo,bar,baz) )
rsync "$excludes[@]"
Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).
I think the following should work for you:
excludes=( $(--exclude=foo,bar,baz) )
rsync "$excludes[@]"
edited Jul 8 at 13:33
answered Jul 4 at 14:46
Jesse_b
10.1k22658
10.1k22658
This does not work. This expands torsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
â ollien
Jul 4 at 15:03
@ollien: That is what you need.--exclude=foo,bar,baz
would be a syntax error to rsync
â Jesse_b
Jul 4 at 15:07
rsync does not recognize the flag when it is prepended by a single quote.
â ollien
Jul 4 at 15:09
The single quote doesn't actually go to rsync, it will be consumed by the shell
â Jesse_b
Jul 4 at 15:10
1
You definitely do not need theecho
in there.excl=( --exclude=a,b,c )
works inbash
.
â Kusalananda
Jul 8 at 12:24
 |Â
show 1 more comment
This does not work. This expands torsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
â ollien
Jul 4 at 15:03
@ollien: That is what you need.--exclude=foo,bar,baz
would be a syntax error to rsync
â Jesse_b
Jul 4 at 15:07
rsync does not recognize the flag when it is prepended by a single quote.
â ollien
Jul 4 at 15:09
The single quote doesn't actually go to rsync, it will be consumed by the shell
â Jesse_b
Jul 4 at 15:10
1
You definitely do not need theecho
in there.excl=( --exclude=a,b,c )
works inbash
.
â Kusalananda
Jul 8 at 12:24
This does not work. This expands to
rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
â ollien
Jul 4 at 15:03
This does not work. This expands to
rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
â ollien
Jul 4 at 15:03
@ollien: That is what you need.
--exclude=foo,bar,baz
would be a syntax error to rsyncâ Jesse_b
Jul 4 at 15:07
@ollien: That is what you need.
--exclude=foo,bar,baz
would be a syntax error to rsyncâ Jesse_b
Jul 4 at 15:07
rsync does not recognize the flag when it is prepended by a single quote.
â ollien
Jul 4 at 15:09
rsync does not recognize the flag when it is prepended by a single quote.
â ollien
Jul 4 at 15:09
The single quote doesn't actually go to rsync, it will be consumed by the shell
â Jesse_b
Jul 4 at 15:10
The single quote doesn't actually go to rsync, it will be consumed by the shell
â Jesse_b
Jul 4 at 15:10
1
1
You definitely do not need the
echo
in there. excl=( --exclude=a,b,c )
works in bash
.â Kusalananda
Jul 8 at 12:24
You definitely do not need the
echo
in there. excl=( --exclude=a,b,c )
works in bash
.â Kusalananda
Jul 8 at 12:24
 |Â
show 1 more comment
up vote
1
down vote
From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.
If you mean the xtrace
output (set -x
), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.
That doesn't mean the quotes are part of the string, though.
Compare:
$ echo abc def ghi
+ echo abc 'def ghi'
abc def ghi
$ echo abc 'def ghi'
+ echo abc ''''def ghi''''
abc 'def ghi'
Though as far as I can see, it doesn't bother to quote strings containing the equal sign.
add a comment |Â
up vote
1
down vote
From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.
If you mean the xtrace
output (set -x
), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.
That doesn't mean the quotes are part of the string, though.
Compare:
$ echo abc def ghi
+ echo abc 'def ghi'
abc def ghi
$ echo abc 'def ghi'
+ echo abc ''''def ghi''''
abc 'def ghi'
Though as far as I can see, it doesn't bother to quote strings containing the equal sign.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.
If you mean the xtrace
output (set -x
), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.
That doesn't mean the quotes are part of the string, though.
Compare:
$ echo abc def ghi
+ echo abc 'def ghi'
abc def ghi
$ echo abc 'def ghi'
+ echo abc ''''def ghi''''
abc 'def ghi'
Though as far as I can see, it doesn't bother to quote strings containing the equal sign.
From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.
If you mean the xtrace
output (set -x
), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.
That doesn't mean the quotes are part of the string, though.
Compare:
$ echo abc def ghi
+ echo abc 'def ghi'
abc def ghi
$ echo abc 'def ghi'
+ echo abc ''''def ghi''''
abc 'def ghi'
Though as far as I can see, it doesn't bother to quote strings containing the equal sign.
answered Jul 4 at 17:25
ilkkachu
47.3k668130
47.3k668130
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453442%2fexpanding-a-variable-containing-an-equals-sign-and-curly-braces%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Do you really want it to expand to
--exclude=foo,bar,baz
or are you trying to have brace expansion expand it to:--exclude=foo --exclude=bar --exclude=baz
?â Jesse_b
Jul 4 at 14:35
The former is the goal.
â ollien
Jul 4 at 14:35
I also think that isn't the right syntax. Should be
--exclude foo
. I don't think rsync takes braces either though.â Jesse_b
Jul 4 at 14:36
Using the equals sign is the correct syntax.
â ollien
Jul 4 at 14:36
From the man page,
--exclude=PATTERN exclude files matching PATTERN
â ollien
Jul 4 at 14:38