Brace Expansion in Quotes
I understand that brace expansion in Bash will expand to multiple arguments. Can I have it expand to a single argument?
Is there a pure solution to this in Bash, without resorting to the nested echo?
Actual Behaviour
command arg{1,2,3}
$1 = arg1
$2 = arg2
$3 = arg3
Desired Behaviour
$1 = arg1 arg2 arg3
$2 =
$3 =
Non-Solution
command "arg{1,2,3}"
$1 = arg{1,2,3}
$2 =
$3 =
Potential Solution (a little hack-y?)
command "$(echo arg{1,2,3})"
$1 = arg1 arg2 arg3
$2 =
$3 =
bash
add a comment |
I understand that brace expansion in Bash will expand to multiple arguments. Can I have it expand to a single argument?
Is there a pure solution to this in Bash, without resorting to the nested echo?
Actual Behaviour
command arg{1,2,3}
$1 = arg1
$2 = arg2
$3 = arg3
Desired Behaviour
$1 = arg1 arg2 arg3
$2 =
$3 =
Non-Solution
command "arg{1,2,3}"
$1 = arg{1,2,3}
$2 =
$3 =
Potential Solution (a little hack-y?)
command "$(echo arg{1,2,3})"
$1 = arg1 arg2 arg3
$2 =
$3 =
bash
add a comment |
I understand that brace expansion in Bash will expand to multiple arguments. Can I have it expand to a single argument?
Is there a pure solution to this in Bash, without resorting to the nested echo?
Actual Behaviour
command arg{1,2,3}
$1 = arg1
$2 = arg2
$3 = arg3
Desired Behaviour
$1 = arg1 arg2 arg3
$2 =
$3 =
Non-Solution
command "arg{1,2,3}"
$1 = arg{1,2,3}
$2 =
$3 =
Potential Solution (a little hack-y?)
command "$(echo arg{1,2,3})"
$1 = arg1 arg2 arg3
$2 =
$3 =
bash
I understand that brace expansion in Bash will expand to multiple arguments. Can I have it expand to a single argument?
Is there a pure solution to this in Bash, without resorting to the nested echo?
Actual Behaviour
command arg{1,2,3}
$1 = arg1
$2 = arg2
$3 = arg3
Desired Behaviour
$1 = arg1 arg2 arg3
$2 =
$3 =
Non-Solution
command "arg{1,2,3}"
$1 = arg{1,2,3}
$2 =
$3 =
Potential Solution (a little hack-y?)
command "$(echo arg{1,2,3})"
$1 = arg1 arg2 arg3
$2 =
$3 =
bash
bash
edited Feb 2 at 22:45
Jared Brandt
asked Feb 2 at 22:40
Jared BrandtJared Brandt
306
306
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Expanding arg{1,2,3} to arg1 arg2 arg3 is called Brace Expansion, splitting that into three words is called Word Splitting and inserting a command’s output Command Substitution. man bash says:
The order of expansions is: brace expansion; tilde expansion,
parameter and variable
expansion, arithmetic expansion, and command substitution (done in a left-to-right
fashion); word splitting; and pathname expansion.
In a command line like
printf %s\n {1..3}
Brace Expansion happens first and makes it
printf %s\n 1 2 3
where 1, 2 and 3 are separate words thanks to Word Splitting. There’s nothing you can do to change this order – as you experienced, quoting it does not only prevent Word Splitting, but Brace Expansion as well. You can however use Command Substitution to get a Brace Expansion even inside double quotes:
If the [command] substitution appears within double quotes, word splitting
and pathname expansion are not performed on the results.
In
printf %s\n "$(echo {1..3})"
the inner of the quotes is expanded first
printf %s\n "$(echo 1 2 3)"
printf %s\n "1 2 3"
and as the result is quoted it is not subject to Word Splitting:
$ printf %s\n {1..3} "{1..3}" "$(echo {1..3})" # prints one argument per line
1
2
3
{1..3}
1 2 3
This is indeed not only the simplest way to do this, but also a pure bash solution, as echo is a bash builtin.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1115110%2fbrace-expansion-in-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Expanding arg{1,2,3} to arg1 arg2 arg3 is called Brace Expansion, splitting that into three words is called Word Splitting and inserting a command’s output Command Substitution. man bash says:
The order of expansions is: brace expansion; tilde expansion,
parameter and variable
expansion, arithmetic expansion, and command substitution (done in a left-to-right
fashion); word splitting; and pathname expansion.
In a command line like
printf %s\n {1..3}
Brace Expansion happens first and makes it
printf %s\n 1 2 3
where 1, 2 and 3 are separate words thanks to Word Splitting. There’s nothing you can do to change this order – as you experienced, quoting it does not only prevent Word Splitting, but Brace Expansion as well. You can however use Command Substitution to get a Brace Expansion even inside double quotes:
If the [command] substitution appears within double quotes, word splitting
and pathname expansion are not performed on the results.
In
printf %s\n "$(echo {1..3})"
the inner of the quotes is expanded first
printf %s\n "$(echo 1 2 3)"
printf %s\n "1 2 3"
and as the result is quoted it is not subject to Word Splitting:
$ printf %s\n {1..3} "{1..3}" "$(echo {1..3})" # prints one argument per line
1
2
3
{1..3}
1 2 3
This is indeed not only the simplest way to do this, but also a pure bash solution, as echo is a bash builtin.
add a comment |
Expanding arg{1,2,3} to arg1 arg2 arg3 is called Brace Expansion, splitting that into three words is called Word Splitting and inserting a command’s output Command Substitution. man bash says:
The order of expansions is: brace expansion; tilde expansion,
parameter and variable
expansion, arithmetic expansion, and command substitution (done in a left-to-right
fashion); word splitting; and pathname expansion.
In a command line like
printf %s\n {1..3}
Brace Expansion happens first and makes it
printf %s\n 1 2 3
where 1, 2 and 3 are separate words thanks to Word Splitting. There’s nothing you can do to change this order – as you experienced, quoting it does not only prevent Word Splitting, but Brace Expansion as well. You can however use Command Substitution to get a Brace Expansion even inside double quotes:
If the [command] substitution appears within double quotes, word splitting
and pathname expansion are not performed on the results.
In
printf %s\n "$(echo {1..3})"
the inner of the quotes is expanded first
printf %s\n "$(echo 1 2 3)"
printf %s\n "1 2 3"
and as the result is quoted it is not subject to Word Splitting:
$ printf %s\n {1..3} "{1..3}" "$(echo {1..3})" # prints one argument per line
1
2
3
{1..3}
1 2 3
This is indeed not only the simplest way to do this, but also a pure bash solution, as echo is a bash builtin.
add a comment |
Expanding arg{1,2,3} to arg1 arg2 arg3 is called Brace Expansion, splitting that into three words is called Word Splitting and inserting a command’s output Command Substitution. man bash says:
The order of expansions is: brace expansion; tilde expansion,
parameter and variable
expansion, arithmetic expansion, and command substitution (done in a left-to-right
fashion); word splitting; and pathname expansion.
In a command line like
printf %s\n {1..3}
Brace Expansion happens first and makes it
printf %s\n 1 2 3
where 1, 2 and 3 are separate words thanks to Word Splitting. There’s nothing you can do to change this order – as you experienced, quoting it does not only prevent Word Splitting, but Brace Expansion as well. You can however use Command Substitution to get a Brace Expansion even inside double quotes:
If the [command] substitution appears within double quotes, word splitting
and pathname expansion are not performed on the results.
In
printf %s\n "$(echo {1..3})"
the inner of the quotes is expanded first
printf %s\n "$(echo 1 2 3)"
printf %s\n "1 2 3"
and as the result is quoted it is not subject to Word Splitting:
$ printf %s\n {1..3} "{1..3}" "$(echo {1..3})" # prints one argument per line
1
2
3
{1..3}
1 2 3
This is indeed not only the simplest way to do this, but also a pure bash solution, as echo is a bash builtin.
Expanding arg{1,2,3} to arg1 arg2 arg3 is called Brace Expansion, splitting that into three words is called Word Splitting and inserting a command’s output Command Substitution. man bash says:
The order of expansions is: brace expansion; tilde expansion,
parameter and variable
expansion, arithmetic expansion, and command substitution (done in a left-to-right
fashion); word splitting; and pathname expansion.
In a command line like
printf %s\n {1..3}
Brace Expansion happens first and makes it
printf %s\n 1 2 3
where 1, 2 and 3 are separate words thanks to Word Splitting. There’s nothing you can do to change this order – as you experienced, quoting it does not only prevent Word Splitting, but Brace Expansion as well. You can however use Command Substitution to get a Brace Expansion even inside double quotes:
If the [command] substitution appears within double quotes, word splitting
and pathname expansion are not performed on the results.
In
printf %s\n "$(echo {1..3})"
the inner of the quotes is expanded first
printf %s\n "$(echo 1 2 3)"
printf %s\n "1 2 3"
and as the result is quoted it is not subject to Word Splitting:
$ printf %s\n {1..3} "{1..3}" "$(echo {1..3})" # prints one argument per line
1
2
3
{1..3}
1 2 3
This is indeed not only the simplest way to do this, but also a pure bash solution, as echo is a bash builtin.
edited Feb 3 at 10:13
answered Feb 3 at 9:57
dessertdessert
24.7k672105
24.7k672105
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1115110%2fbrace-expansion-in-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown