How to get an alternate separator for brace expansion?2019 Community Moderator ElectionGet the source directory of a Bash script from within the script itselfHow do I parse command line arguments in Bash?How to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?How to count all the lines of code in a directory recursively?How to use double or single brackets, parentheses, curly bracesHow do I reload .bashrc without logging out and back in?How to concatenate string variables in Bash
How to read string as hex number in bash?
Is "inadequate referencing" a euphemism for plagiarism?
How are passwords stolen from companies if they only store hashes?
Print last inputted byte
How do you justify more code being written by following clean code practices?
Hot air balloons as primitive bombers
Imaginary part of expression too difficult to calculate
How old is Nick Fury?
When should a starting writer get his own webpage?
"Marked down as someone wanting to sell shares." What does that mean?
The English Debate
How can an organ that provides biological immortality be unable to regenerate?
Would this string work as string?
Nested Dynamic SOQL Query
Air travel with refrigerated insulin
Hackerrank All Women's Codesprint 2019: Name the Product
Pre-Employment Background Check With Consent For Future Checks
Why doesn't the fusion process of the sun speed up?
Weird lines in Microsoft Word
Can "few" be used as a subject? If so, what is the rule?
Help with identifying unique aircraft over NE Pennsylvania
PTIJ: Which Dr. Seuss books should one obtain?
What are the rules for concealing thieves' tools (or items in general)?
What kind of footwear is suitable for walking in micro gravity environment?
How to get an alternate separator for brace expansion?
2019 Community Moderator ElectionGet the source directory of a Bash script from within the script itselfHow do I parse command line arguments in Bash?How to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?How to count all the lines of code in a directory recursively?How to use double or single brackets, parentheses, curly bracesHow do I reload .bashrc without logging out and back in?How to concatenate string variables in Bash
I find myself often doing silly things like:
cmd $( echo foo1,2,3 | tr ' ' , )
when I want to invoke cmd foo1,foo2,foo3
. I would like instead to be able to do something along the lines of:
OFS=, ; cmd foo1,2,3
to force the brace expansion to give me a comma separated list. Is there any such functionality available?
bash
add a comment |
I find myself often doing silly things like:
cmd $( echo foo1,2,3 | tr ' ' , )
when I want to invoke cmd foo1,foo2,foo3
. I would like instead to be able to do something along the lines of:
OFS=, ; cmd foo1,2,3
to force the brace expansion to give me a comma separated list. Is there any such functionality available?
bash
Ifcmd
is always the same, a function might help.
– Cyrus
Mar 7 at 18:51
Interestingly, the documentation for neither brace expansion nor pathname expansion (to which brace expansion is compared) ever actually says what the result is. It just says a "list" of words.
– chepner
Mar 7 at 18:52
The closest thing I know of would be a clumsy use of an array.cmd "$(IFS=,; x=(foo1,2,3); printf '%s' "$x[*]")"
.
– chepner
Mar 7 at 18:55
add a comment |
I find myself often doing silly things like:
cmd $( echo foo1,2,3 | tr ' ' , )
when I want to invoke cmd foo1,foo2,foo3
. I would like instead to be able to do something along the lines of:
OFS=, ; cmd foo1,2,3
to force the brace expansion to give me a comma separated list. Is there any such functionality available?
bash
I find myself often doing silly things like:
cmd $( echo foo1,2,3 | tr ' ' , )
when I want to invoke cmd foo1,foo2,foo3
. I would like instead to be able to do something along the lines of:
OFS=, ; cmd foo1,2,3
to force the brace expansion to give me a comma separated list. Is there any such functionality available?
bash
bash
asked Mar 7 at 18:43
William PursellWilliam Pursell
133k32206240
133k32206240
Ifcmd
is always the same, a function might help.
– Cyrus
Mar 7 at 18:51
Interestingly, the documentation for neither brace expansion nor pathname expansion (to which brace expansion is compared) ever actually says what the result is. It just says a "list" of words.
– chepner
Mar 7 at 18:52
The closest thing I know of would be a clumsy use of an array.cmd "$(IFS=,; x=(foo1,2,3); printf '%s' "$x[*]")"
.
– chepner
Mar 7 at 18:55
add a comment |
Ifcmd
is always the same, a function might help.
– Cyrus
Mar 7 at 18:51
Interestingly, the documentation for neither brace expansion nor pathname expansion (to which brace expansion is compared) ever actually says what the result is. It just says a "list" of words.
– chepner
Mar 7 at 18:52
The closest thing I know of would be a clumsy use of an array.cmd "$(IFS=,; x=(foo1,2,3); printf '%s' "$x[*]")"
.
– chepner
Mar 7 at 18:55
If
cmd
is always the same, a function might help.– Cyrus
Mar 7 at 18:51
If
cmd
is always the same, a function might help.– Cyrus
Mar 7 at 18:51
Interestingly, the documentation for neither brace expansion nor pathname expansion (to which brace expansion is compared) ever actually says what the result is. It just says a "list" of words.
– chepner
Mar 7 at 18:52
Interestingly, the documentation for neither brace expansion nor pathname expansion (to which brace expansion is compared) ever actually says what the result is. It just says a "list" of words.
– chepner
Mar 7 at 18:52
The closest thing I know of would be a clumsy use of an array.
cmd "$(IFS=,; x=(foo1,2,3); printf '%s' "$x[*]")"
.– chepner
Mar 7 at 18:55
The closest thing I know of would be a clumsy use of an array.
cmd "$(IFS=,; x=(foo1,2,3); printf '%s' "$x[*]")"
.– chepner
Mar 7 at 18:55
add a comment |
2 Answers
2
active
oldest
votes
I don't think it's possible with a simple variable or setting.
However, you can capture the brace expansion in an array, and then use IFS and parameter substitution to achieve that effect:
$ ( a=( foo1,2,3 ); IFS=,; printf -- "%sn" "$a[*]" )
foo1,foo2,foo3
There, I'm using a subshell so I don't alter IFS in the current shell.
Here's a short
function that's much tidier:
$ join() local IFS=$1; shift; printf -- "%sn" "$*";
$ join , foo1,2,3
foo1,foo2,foo3
add a comment |
You can create a function that will echo a comma-separated list of its args:
commas() local IFS=,; echo "$*";
Then you can use it in your calls to cmd
:
cmd "$(commas foo1,2,3)"
Edit: Note that echo
is somewhat broken, in that if you pass a single arg that looks like a valid echo
options (e.g., -e
, -n
, etc), echo
will consume it and interpret it as an option instead of printing it.
$ commas -e #prints nothing
$ commas -n #prints nothing
$ commas -e -n
-e,-n
The last one works because "$*"
expands to -e,-n
, which echo
sees as a single argument.
And echo
doesn't respect the --
argument as an "end of options" indicator like printf
does. So you may want to use printf -- "%sn" "$*"
, as in @glennjackman's answer. Then again, the whole point of the commas
function is to combine multiple args into a comma-separated list, as a single string. So it's probably unlikely that you'd run into any issues with echo
. But hey, it only costs you a few more characters to type printf -- "%s"
instead of echo
.
The unquoted command substitution can cause problems, depending on what the arguments tocommas
end up being.
– chepner
Mar 7 at 18:59
@Aaron Fixed. Thanks.
– Mike Holt
Mar 7 at 18:59
@chepner Well spotted. Thanks.
– Mike Holt
Mar 7 at 19:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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%2fstackoverflow.com%2fquestions%2f55050760%2fhow-to-get-an-alternate-separator-for-brace-expansion%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think it's possible with a simple variable or setting.
However, you can capture the brace expansion in an array, and then use IFS and parameter substitution to achieve that effect:
$ ( a=( foo1,2,3 ); IFS=,; printf -- "%sn" "$a[*]" )
foo1,foo2,foo3
There, I'm using a subshell so I don't alter IFS in the current shell.
Here's a short
function that's much tidier:
$ join() local IFS=$1; shift; printf -- "%sn" "$*";
$ join , foo1,2,3
foo1,foo2,foo3
add a comment |
I don't think it's possible with a simple variable or setting.
However, you can capture the brace expansion in an array, and then use IFS and parameter substitution to achieve that effect:
$ ( a=( foo1,2,3 ); IFS=,; printf -- "%sn" "$a[*]" )
foo1,foo2,foo3
There, I'm using a subshell so I don't alter IFS in the current shell.
Here's a short
function that's much tidier:
$ join() local IFS=$1; shift; printf -- "%sn" "$*";
$ join , foo1,2,3
foo1,foo2,foo3
add a comment |
I don't think it's possible with a simple variable or setting.
However, you can capture the brace expansion in an array, and then use IFS and parameter substitution to achieve that effect:
$ ( a=( foo1,2,3 ); IFS=,; printf -- "%sn" "$a[*]" )
foo1,foo2,foo3
There, I'm using a subshell so I don't alter IFS in the current shell.
Here's a short
function that's much tidier:
$ join() local IFS=$1; shift; printf -- "%sn" "$*";
$ join , foo1,2,3
foo1,foo2,foo3
I don't think it's possible with a simple variable or setting.
However, you can capture the brace expansion in an array, and then use IFS and parameter substitution to achieve that effect:
$ ( a=( foo1,2,3 ); IFS=,; printf -- "%sn" "$a[*]" )
foo1,foo2,foo3
There, I'm using a subshell so I don't alter IFS in the current shell.
Here's a short
function that's much tidier:
$ join() local IFS=$1; shift; printf -- "%sn" "$*";
$ join , foo1,2,3
foo1,foo2,foo3
answered Mar 7 at 18:55
glenn jackmanglenn jackman
170k26147240
170k26147240
add a comment |
add a comment |
You can create a function that will echo a comma-separated list of its args:
commas() local IFS=,; echo "$*";
Then you can use it in your calls to cmd
:
cmd "$(commas foo1,2,3)"
Edit: Note that echo
is somewhat broken, in that if you pass a single arg that looks like a valid echo
options (e.g., -e
, -n
, etc), echo
will consume it and interpret it as an option instead of printing it.
$ commas -e #prints nothing
$ commas -n #prints nothing
$ commas -e -n
-e,-n
The last one works because "$*"
expands to -e,-n
, which echo
sees as a single argument.
And echo
doesn't respect the --
argument as an "end of options" indicator like printf
does. So you may want to use printf -- "%sn" "$*"
, as in @glennjackman's answer. Then again, the whole point of the commas
function is to combine multiple args into a comma-separated list, as a single string. So it's probably unlikely that you'd run into any issues with echo
. But hey, it only costs you a few more characters to type printf -- "%s"
instead of echo
.
The unquoted command substitution can cause problems, depending on what the arguments tocommas
end up being.
– chepner
Mar 7 at 18:59
@Aaron Fixed. Thanks.
– Mike Holt
Mar 7 at 18:59
@chepner Well spotted. Thanks.
– Mike Holt
Mar 7 at 19:00
add a comment |
You can create a function that will echo a comma-separated list of its args:
commas() local IFS=,; echo "$*";
Then you can use it in your calls to cmd
:
cmd "$(commas foo1,2,3)"
Edit: Note that echo
is somewhat broken, in that if you pass a single arg that looks like a valid echo
options (e.g., -e
, -n
, etc), echo
will consume it and interpret it as an option instead of printing it.
$ commas -e #prints nothing
$ commas -n #prints nothing
$ commas -e -n
-e,-n
The last one works because "$*"
expands to -e,-n
, which echo
sees as a single argument.
And echo
doesn't respect the --
argument as an "end of options" indicator like printf
does. So you may want to use printf -- "%sn" "$*"
, as in @glennjackman's answer. Then again, the whole point of the commas
function is to combine multiple args into a comma-separated list, as a single string. So it's probably unlikely that you'd run into any issues with echo
. But hey, it only costs you a few more characters to type printf -- "%s"
instead of echo
.
The unquoted command substitution can cause problems, depending on what the arguments tocommas
end up being.
– chepner
Mar 7 at 18:59
@Aaron Fixed. Thanks.
– Mike Holt
Mar 7 at 18:59
@chepner Well spotted. Thanks.
– Mike Holt
Mar 7 at 19:00
add a comment |
You can create a function that will echo a comma-separated list of its args:
commas() local IFS=,; echo "$*";
Then you can use it in your calls to cmd
:
cmd "$(commas foo1,2,3)"
Edit: Note that echo
is somewhat broken, in that if you pass a single arg that looks like a valid echo
options (e.g., -e
, -n
, etc), echo
will consume it and interpret it as an option instead of printing it.
$ commas -e #prints nothing
$ commas -n #prints nothing
$ commas -e -n
-e,-n
The last one works because "$*"
expands to -e,-n
, which echo
sees as a single argument.
And echo
doesn't respect the --
argument as an "end of options" indicator like printf
does. So you may want to use printf -- "%sn" "$*"
, as in @glennjackman's answer. Then again, the whole point of the commas
function is to combine multiple args into a comma-separated list, as a single string. So it's probably unlikely that you'd run into any issues with echo
. But hey, it only costs you a few more characters to type printf -- "%s"
instead of echo
.
You can create a function that will echo a comma-separated list of its args:
commas() local IFS=,; echo "$*";
Then you can use it in your calls to cmd
:
cmd "$(commas foo1,2,3)"
Edit: Note that echo
is somewhat broken, in that if you pass a single arg that looks like a valid echo
options (e.g., -e
, -n
, etc), echo
will consume it and interpret it as an option instead of printing it.
$ commas -e #prints nothing
$ commas -n #prints nothing
$ commas -e -n
-e,-n
The last one works because "$*"
expands to -e,-n
, which echo
sees as a single argument.
And echo
doesn't respect the --
argument as an "end of options" indicator like printf
does. So you may want to use printf -- "%sn" "$*"
, as in @glennjackman's answer. Then again, the whole point of the commas
function is to combine multiple args into a comma-separated list, as a single string. So it's probably unlikely that you'd run into any issues with echo
. But hey, it only costs you a few more characters to type printf -- "%s"
instead of echo
.
edited Mar 7 at 19:23
answered Mar 7 at 18:56
Mike HoltMike Holt
2,9021920
2,9021920
The unquoted command substitution can cause problems, depending on what the arguments tocommas
end up being.
– chepner
Mar 7 at 18:59
@Aaron Fixed. Thanks.
– Mike Holt
Mar 7 at 18:59
@chepner Well spotted. Thanks.
– Mike Holt
Mar 7 at 19:00
add a comment |
The unquoted command substitution can cause problems, depending on what the arguments tocommas
end up being.
– chepner
Mar 7 at 18:59
@Aaron Fixed. Thanks.
– Mike Holt
Mar 7 at 18:59
@chepner Well spotted. Thanks.
– Mike Holt
Mar 7 at 19:00
The unquoted command substitution can cause problems, depending on what the arguments to
commas
end up being.– chepner
Mar 7 at 18:59
The unquoted command substitution can cause problems, depending on what the arguments to
commas
end up being.– chepner
Mar 7 at 18:59
@Aaron Fixed. Thanks.
– Mike Holt
Mar 7 at 18:59
@Aaron Fixed. Thanks.
– Mike Holt
Mar 7 at 18:59
@chepner Well spotted. Thanks.
– Mike Holt
Mar 7 at 19:00
@chepner Well spotted. Thanks.
– Mike Holt
Mar 7 at 19:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55050760%2fhow-to-get-an-alternate-separator-for-brace-expansion%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
If
cmd
is always the same, a function might help.– Cyrus
Mar 7 at 18:51
Interestingly, the documentation for neither brace expansion nor pathname expansion (to which brace expansion is compared) ever actually says what the result is. It just says a "list" of words.
– chepner
Mar 7 at 18:52
The closest thing I know of would be a clumsy use of an array.
cmd "$(IFS=,; x=(foo1,2,3); printf '%s' "$x[*]")"
.– chepner
Mar 7 at 18:55