What is the difference between `a[bc]d` (brackets) and `ab,cd` (braces)?Using curly brackets (braces) to create folder structure with `mkdir -p`Short way to scp using the same dir/file in origin and targetQuoting curly braces in the shellIs there a Unix command that searches for similar strings, based mostly on how they sound when spoken?Bash expansion of $@ as commandHow can I suppress the space between generated arguments during brace expansion?Bash decimal to binary conversion explanationBraces don't work when there is a single elementBrace expansion and compound commandWhy doesn't systemctl restart,status sshd; work?
How do we know that ממחרת השבת means from the first day of pesach and not the seventh?
Who and which - What to choose when we are referring to a choice between two or more things or persons
Mac Pro install disk keeps ejecting itself
How to verbalise code in Mathematica?
Killing undead fish underwater
Fizzy, soft, pop and still drinks
Reverse the word in a string with the same order in javascript
Question relating to a number theoretic function
Why was Germany not as successful as other Europeans in establishing overseas colonies?
Rivers without rain
how to interpret this t result?
How to back up a running remote server?
How to sum variables from file in Bash
What is the most expensive material in the world that could be used to create Pun-Pun's lute?
Will a top journal at least read my introduction?
Single SMS won't delete from Messaging app
Can someone publish a story that happened to you?
What's the polite way to say "I need to urinate"?
How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?
French for 'It must be my imagination'?
How could Tony Stark make this in Endgame?
How come there are so many candidates for the 2020 Democratic party presidential nomination?
Does this extra sentence in the description of the warlock's Eyes of the Rune Keeper eldritch invocation appear in any official reference?
Was there a shared-world project before "Thieves World"?
What is the difference between `a[bc]d` (brackets) and `ab,cd` (braces)?
Using curly brackets (braces) to create folder structure with `mkdir -p`Short way to scp using the same dir/file in origin and targetQuoting curly braces in the shellIs there a Unix command that searches for similar strings, based mostly on how they sound when spoken?Bash expansion of $@ as commandHow can I suppress the space between generated arguments during brace expansion?Bash decimal to binary conversion explanationBraces don't work when there is a single elementBrace expansion and compound commandWhy doesn't systemctl restart,status sshd; work?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
What is the difference between a[bc]d and ab,cd? Why do people use ab,cd when there is already a[bc]d?
brace-expansion pattern-matching
add a comment |
What is the difference between a[bc]d and ab,cd? Why do people use ab,cd when there is already a[bc]d?
brace-expansion pattern-matching
Who told you to usecommand a[bc]d?
– Jesse_b
18 hours ago
2
It certainly has its uses if one understands it correctly.
– Weijun Zhou
18 hours ago
2
I guess I just don't understand how the confusion between the two happened.
– Jesse_b
17 hours ago
I have been explicitly asked by a coworker less familiar with Linux on this, although not recently.
– Weijun Zhou
17 hours ago
add a comment |
What is the difference between a[bc]d and ab,cd? Why do people use ab,cd when there is already a[bc]d?
brace-expansion pattern-matching
What is the difference between a[bc]d and ab,cd? Why do people use ab,cd when there is already a[bc]d?
brace-expansion pattern-matching
brace-expansion pattern-matching
edited 19 mins ago
muru
38.1k591166
38.1k591166
asked 18 hours ago
Weijun ZhouWeijun Zhou
1,842429
1,842429
Who told you to usecommand a[bc]d?
– Jesse_b
18 hours ago
2
It certainly has its uses if one understands it correctly.
– Weijun Zhou
18 hours ago
2
I guess I just don't understand how the confusion between the two happened.
– Jesse_b
17 hours ago
I have been explicitly asked by a coworker less familiar with Linux on this, although not recently.
– Weijun Zhou
17 hours ago
add a comment |
Who told you to usecommand a[bc]d?
– Jesse_b
18 hours ago
2
It certainly has its uses if one understands it correctly.
– Weijun Zhou
18 hours ago
2
I guess I just don't understand how the confusion between the two happened.
– Jesse_b
17 hours ago
I have been explicitly asked by a coworker less familiar with Linux on this, although not recently.
– Weijun Zhou
17 hours ago
Who told you to use
command a[bc]d?– Jesse_b
18 hours ago
Who told you to use
command a[bc]d?– Jesse_b
18 hours ago
2
2
It certainly has its uses if one understands it correctly.
– Weijun Zhou
18 hours ago
It certainly has its uses if one understands it correctly.
– Weijun Zhou
18 hours ago
2
2
I guess I just don't understand how the confusion between the two happened.
– Jesse_b
17 hours ago
I guess I just don't understand how the confusion between the two happened.
– Jesse_b
17 hours ago
I have been explicitly asked by a coworker less familiar with Linux on this, although not recently.
– Weijun Zhou
17 hours ago
I have been explicitly asked by a coworker less familiar with Linux on this, although not recently.
– Weijun Zhou
17 hours ago
add a comment |
2 Answers
2
active
oldest
votes
The two are quite different.
a[bc]d is a filename pattern. It will expand to the two filenames abd and acd if those are names of existing files in the current directory.
The
[...]part is a bracketed expression that matches a single character out of the ones listed. In this pattern, the character betweenaanddmust be either abor acfor the pattern to match.If
abdexists, butacddoes not, then it would only expand toabd, and vice versa. By default, the pattern would remain unexpanded if there was no matching filename.
ab,cd is a brace expansion (in shells that support these). It will expand to the two strings abd and acd.
The
...part is a comma-delimited set of strings (in this example; it may also be a range such asa..kor20..25), and the expansion is computed by combining each of these strings with the flanking stringsaandd.The expansion happens regardless of whether these strings corresponds to existing filenames or not.
If you are constructing strings, use a brace expansion. If you are matching filenames, use a filename pattern.
Thank you for clarifying "If abd exists, but acd does not, then it would only expand to abd". I guess that is what is missing from my answer.
– Weijun Zhou
16 hours ago
1
Another crucial difference is that inab,cd, thebandcparts don't need to be single letters; e.g.exten,cision. Whileex[tenci]sionor whatever will only match one of these letters.
– alexis
1 hour ago
@alexis Thanks, I've made some changes.
– Kusalananda♦
54 mins ago
add a comment |
a[bc]d is pattern-matching, and is part of the POSIX standard. In POSIX, this is introduced as the "pattern bracket expression". It is documented in section 2.13 of the manual
When unquoted and outside a bracket expression, the following three
characters shall have special meaning in the specification of
patterns:
?
A question-mark is a pattern that shall match any character.
*
An asterisk is a pattern that shall match multiple characters, as
described in Patterns Matching Multiple Characters.
[
The open bracket shall introduce a pattern bracket expression.
Section 2.13.3 also mentions something that it behaves differently from what one would expect for usual regexs when it is used for filename expansion (emphasis by me)
The rules described so far in Patterns Matching a Single Character and
Patterns Matching Multiple Characters are qualified by the following
rules that apply when pattern matching notation is used for filename
expansion:
The slash character in a pathname shall be explicitly matched by using
one or more slashes in the pattern; it shall neither be matched by the
asterisk or question-mark special characters nor by a bracket
expression. Slashes in the pattern shall be identified before bracket
expressions; thus, a slash cannot be included in a pattern bracket
expression used for filename expansion. If a slash character is found
following an unescaped open square bracket character before a
corresponding closing square bracket is found, the open bracket shall
be treated as an ordinary character. For example, the pattern
"a[b/c]d"does not match such pathnames asabdora/d.
It only matches a pathname of literallya[b/c]d.
ab,cd is braces expansion, it is not in the specification by POSIX. Here is the corresponding part from the bash manual (emphasis by me):
Brace expansion is a mechanism by which arbitrary strings may be
generated. This mechanism is similar to filename expansion (see
Filename Expansion), but the filenames generated need not exist.
Patterns to be brace expanded take the form of an optional preamble,
followed by either a series of comma-separated strings or a sequence
expression between a pair of braces, followed by an optional
postscript. The preamble is prefixed to each string contained within
the braces, and the postscript is then appended to each resulting
string, expanding left to right.
According to the comment by @mosvy, this first appeared from csh but the behavior in bash is different from csh and other shells. This type of braces expansion is also present in glob(3).
There is another type of braces expansion a..z that only appeared after bash 3.0, and there are more added in bash 4.0.
In a shell where globbing is turned on, execute in a empty folder, the following result is returned
$ echo a[bc]d
a[bc]d
$ echo ab,cd
abd acd
In response to @Jesse_b's comment, if you are in an interactive shell and both of them applies, a[bc]d is less trouble typing. For example grep pattern [ab][12].txt.
1
The brace expansion is not a "bashism"; it first appeared incsh, long beforebash. It's also present in the glob(3) library function. The difference is that inbashit's performed before other expansions:a=A; ab=A/B; ac=A/C; echo $ab,cwill work in bash differently from any other shell.
– mosvy
15 hours ago
Thank you. I will update the answer.
– Weijun Zhou
15 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f515895%2fwhat-is-the-difference-between-abcd-brackets-and-ab-cd-braces%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
The two are quite different.
a[bc]d is a filename pattern. It will expand to the two filenames abd and acd if those are names of existing files in the current directory.
The
[...]part is a bracketed expression that matches a single character out of the ones listed. In this pattern, the character betweenaanddmust be either abor acfor the pattern to match.If
abdexists, butacddoes not, then it would only expand toabd, and vice versa. By default, the pattern would remain unexpanded if there was no matching filename.
ab,cd is a brace expansion (in shells that support these). It will expand to the two strings abd and acd.
The
...part is a comma-delimited set of strings (in this example; it may also be a range such asa..kor20..25), and the expansion is computed by combining each of these strings with the flanking stringsaandd.The expansion happens regardless of whether these strings corresponds to existing filenames or not.
If you are constructing strings, use a brace expansion. If you are matching filenames, use a filename pattern.
Thank you for clarifying "If abd exists, but acd does not, then it would only expand to abd". I guess that is what is missing from my answer.
– Weijun Zhou
16 hours ago
1
Another crucial difference is that inab,cd, thebandcparts don't need to be single letters; e.g.exten,cision. Whileex[tenci]sionor whatever will only match one of these letters.
– alexis
1 hour ago
@alexis Thanks, I've made some changes.
– Kusalananda♦
54 mins ago
add a comment |
The two are quite different.
a[bc]d is a filename pattern. It will expand to the two filenames abd and acd if those are names of existing files in the current directory.
The
[...]part is a bracketed expression that matches a single character out of the ones listed. In this pattern, the character betweenaanddmust be either abor acfor the pattern to match.If
abdexists, butacddoes not, then it would only expand toabd, and vice versa. By default, the pattern would remain unexpanded if there was no matching filename.
ab,cd is a brace expansion (in shells that support these). It will expand to the two strings abd and acd.
The
...part is a comma-delimited set of strings (in this example; it may also be a range such asa..kor20..25), and the expansion is computed by combining each of these strings with the flanking stringsaandd.The expansion happens regardless of whether these strings corresponds to existing filenames or not.
If you are constructing strings, use a brace expansion. If you are matching filenames, use a filename pattern.
Thank you for clarifying "If abd exists, but acd does not, then it would only expand to abd". I guess that is what is missing from my answer.
– Weijun Zhou
16 hours ago
1
Another crucial difference is that inab,cd, thebandcparts don't need to be single letters; e.g.exten,cision. Whileex[tenci]sionor whatever will only match one of these letters.
– alexis
1 hour ago
@alexis Thanks, I've made some changes.
– Kusalananda♦
54 mins ago
add a comment |
The two are quite different.
a[bc]d is a filename pattern. It will expand to the two filenames abd and acd if those are names of existing files in the current directory.
The
[...]part is a bracketed expression that matches a single character out of the ones listed. In this pattern, the character betweenaanddmust be either abor acfor the pattern to match.If
abdexists, butacddoes not, then it would only expand toabd, and vice versa. By default, the pattern would remain unexpanded if there was no matching filename.
ab,cd is a brace expansion (in shells that support these). It will expand to the two strings abd and acd.
The
...part is a comma-delimited set of strings (in this example; it may also be a range such asa..kor20..25), and the expansion is computed by combining each of these strings with the flanking stringsaandd.The expansion happens regardless of whether these strings corresponds to existing filenames or not.
If you are constructing strings, use a brace expansion. If you are matching filenames, use a filename pattern.
The two are quite different.
a[bc]d is a filename pattern. It will expand to the two filenames abd and acd if those are names of existing files in the current directory.
The
[...]part is a bracketed expression that matches a single character out of the ones listed. In this pattern, the character betweenaanddmust be either abor acfor the pattern to match.If
abdexists, butacddoes not, then it would only expand toabd, and vice versa. By default, the pattern would remain unexpanded if there was no matching filename.
ab,cd is a brace expansion (in shells that support these). It will expand to the two strings abd and acd.
The
...part is a comma-delimited set of strings (in this example; it may also be a range such asa..kor20..25), and the expansion is computed by combining each of these strings with the flanking stringsaandd.The expansion happens regardless of whether these strings corresponds to existing filenames or not.
If you are constructing strings, use a brace expansion. If you are matching filenames, use a filename pattern.
edited 54 mins ago
answered 16 hours ago
Kusalananda♦Kusalananda
144k18271450
144k18271450
Thank you for clarifying "If abd exists, but acd does not, then it would only expand to abd". I guess that is what is missing from my answer.
– Weijun Zhou
16 hours ago
1
Another crucial difference is that inab,cd, thebandcparts don't need to be single letters; e.g.exten,cision. Whileex[tenci]sionor whatever will only match one of these letters.
– alexis
1 hour ago
@alexis Thanks, I've made some changes.
– Kusalananda♦
54 mins ago
add a comment |
Thank you for clarifying "If abd exists, but acd does not, then it would only expand to abd". I guess that is what is missing from my answer.
– Weijun Zhou
16 hours ago
1
Another crucial difference is that inab,cd, thebandcparts don't need to be single letters; e.g.exten,cision. Whileex[tenci]sionor whatever will only match one of these letters.
– alexis
1 hour ago
@alexis Thanks, I've made some changes.
– Kusalananda♦
54 mins ago
Thank you for clarifying "If abd exists, but acd does not, then it would only expand to abd". I guess that is what is missing from my answer.
– Weijun Zhou
16 hours ago
Thank you for clarifying "If abd exists, but acd does not, then it would only expand to abd". I guess that is what is missing from my answer.
– Weijun Zhou
16 hours ago
1
1
Another crucial difference is that in
ab,cd, the b and c parts don't need to be single letters; e.g. exten,cision. While ex[tenci]sion or whatever will only match one of these letters.– alexis
1 hour ago
Another crucial difference is that in
ab,cd, the b and c parts don't need to be single letters; e.g. exten,cision. While ex[tenci]sion or whatever will only match one of these letters.– alexis
1 hour ago
@alexis Thanks, I've made some changes.
– Kusalananda♦
54 mins ago
@alexis Thanks, I've made some changes.
– Kusalananda♦
54 mins ago
add a comment |
a[bc]d is pattern-matching, and is part of the POSIX standard. In POSIX, this is introduced as the "pattern bracket expression". It is documented in section 2.13 of the manual
When unquoted and outside a bracket expression, the following three
characters shall have special meaning in the specification of
patterns:
?
A question-mark is a pattern that shall match any character.
*
An asterisk is a pattern that shall match multiple characters, as
described in Patterns Matching Multiple Characters.
[
The open bracket shall introduce a pattern bracket expression.
Section 2.13.3 also mentions something that it behaves differently from what one would expect for usual regexs when it is used for filename expansion (emphasis by me)
The rules described so far in Patterns Matching a Single Character and
Patterns Matching Multiple Characters are qualified by the following
rules that apply when pattern matching notation is used for filename
expansion:
The slash character in a pathname shall be explicitly matched by using
one or more slashes in the pattern; it shall neither be matched by the
asterisk or question-mark special characters nor by a bracket
expression. Slashes in the pattern shall be identified before bracket
expressions; thus, a slash cannot be included in a pattern bracket
expression used for filename expansion. If a slash character is found
following an unescaped open square bracket character before a
corresponding closing square bracket is found, the open bracket shall
be treated as an ordinary character. For example, the pattern
"a[b/c]d"does not match such pathnames asabdora/d.
It only matches a pathname of literallya[b/c]d.
ab,cd is braces expansion, it is not in the specification by POSIX. Here is the corresponding part from the bash manual (emphasis by me):
Brace expansion is a mechanism by which arbitrary strings may be
generated. This mechanism is similar to filename expansion (see
Filename Expansion), but the filenames generated need not exist.
Patterns to be brace expanded take the form of an optional preamble,
followed by either a series of comma-separated strings or a sequence
expression between a pair of braces, followed by an optional
postscript. The preamble is prefixed to each string contained within
the braces, and the postscript is then appended to each resulting
string, expanding left to right.
According to the comment by @mosvy, this first appeared from csh but the behavior in bash is different from csh and other shells. This type of braces expansion is also present in glob(3).
There is another type of braces expansion a..z that only appeared after bash 3.0, and there are more added in bash 4.0.
In a shell where globbing is turned on, execute in a empty folder, the following result is returned
$ echo a[bc]d
a[bc]d
$ echo ab,cd
abd acd
In response to @Jesse_b's comment, if you are in an interactive shell and both of them applies, a[bc]d is less trouble typing. For example grep pattern [ab][12].txt.
1
The brace expansion is not a "bashism"; it first appeared incsh, long beforebash. It's also present in the glob(3) library function. The difference is that inbashit's performed before other expansions:a=A; ab=A/B; ac=A/C; echo $ab,cwill work in bash differently from any other shell.
– mosvy
15 hours ago
Thank you. I will update the answer.
– Weijun Zhou
15 hours ago
add a comment |
a[bc]d is pattern-matching, and is part of the POSIX standard. In POSIX, this is introduced as the "pattern bracket expression". It is documented in section 2.13 of the manual
When unquoted and outside a bracket expression, the following three
characters shall have special meaning in the specification of
patterns:
?
A question-mark is a pattern that shall match any character.
*
An asterisk is a pattern that shall match multiple characters, as
described in Patterns Matching Multiple Characters.
[
The open bracket shall introduce a pattern bracket expression.
Section 2.13.3 also mentions something that it behaves differently from what one would expect for usual regexs when it is used for filename expansion (emphasis by me)
The rules described so far in Patterns Matching a Single Character and
Patterns Matching Multiple Characters are qualified by the following
rules that apply when pattern matching notation is used for filename
expansion:
The slash character in a pathname shall be explicitly matched by using
one or more slashes in the pattern; it shall neither be matched by the
asterisk or question-mark special characters nor by a bracket
expression. Slashes in the pattern shall be identified before bracket
expressions; thus, a slash cannot be included in a pattern bracket
expression used for filename expansion. If a slash character is found
following an unescaped open square bracket character before a
corresponding closing square bracket is found, the open bracket shall
be treated as an ordinary character. For example, the pattern
"a[b/c]d"does not match such pathnames asabdora/d.
It only matches a pathname of literallya[b/c]d.
ab,cd is braces expansion, it is not in the specification by POSIX. Here is the corresponding part from the bash manual (emphasis by me):
Brace expansion is a mechanism by which arbitrary strings may be
generated. This mechanism is similar to filename expansion (see
Filename Expansion), but the filenames generated need not exist.
Patterns to be brace expanded take the form of an optional preamble,
followed by either a series of comma-separated strings or a sequence
expression between a pair of braces, followed by an optional
postscript. The preamble is prefixed to each string contained within
the braces, and the postscript is then appended to each resulting
string, expanding left to right.
According to the comment by @mosvy, this first appeared from csh but the behavior in bash is different from csh and other shells. This type of braces expansion is also present in glob(3).
There is another type of braces expansion a..z that only appeared after bash 3.0, and there are more added in bash 4.0.
In a shell where globbing is turned on, execute in a empty folder, the following result is returned
$ echo a[bc]d
a[bc]d
$ echo ab,cd
abd acd
In response to @Jesse_b's comment, if you are in an interactive shell and both of them applies, a[bc]d is less trouble typing. For example grep pattern [ab][12].txt.
1
The brace expansion is not a "bashism"; it first appeared incsh, long beforebash. It's also present in the glob(3) library function. The difference is that inbashit's performed before other expansions:a=A; ab=A/B; ac=A/C; echo $ab,cwill work in bash differently from any other shell.
– mosvy
15 hours ago
Thank you. I will update the answer.
– Weijun Zhou
15 hours ago
add a comment |
a[bc]d is pattern-matching, and is part of the POSIX standard. In POSIX, this is introduced as the "pattern bracket expression". It is documented in section 2.13 of the manual
When unquoted and outside a bracket expression, the following three
characters shall have special meaning in the specification of
patterns:
?
A question-mark is a pattern that shall match any character.
*
An asterisk is a pattern that shall match multiple characters, as
described in Patterns Matching Multiple Characters.
[
The open bracket shall introduce a pattern bracket expression.
Section 2.13.3 also mentions something that it behaves differently from what one would expect for usual regexs when it is used for filename expansion (emphasis by me)
The rules described so far in Patterns Matching a Single Character and
Patterns Matching Multiple Characters are qualified by the following
rules that apply when pattern matching notation is used for filename
expansion:
The slash character in a pathname shall be explicitly matched by using
one or more slashes in the pattern; it shall neither be matched by the
asterisk or question-mark special characters nor by a bracket
expression. Slashes in the pattern shall be identified before bracket
expressions; thus, a slash cannot be included in a pattern bracket
expression used for filename expansion. If a slash character is found
following an unescaped open square bracket character before a
corresponding closing square bracket is found, the open bracket shall
be treated as an ordinary character. For example, the pattern
"a[b/c]d"does not match such pathnames asabdora/d.
It only matches a pathname of literallya[b/c]d.
ab,cd is braces expansion, it is not in the specification by POSIX. Here is the corresponding part from the bash manual (emphasis by me):
Brace expansion is a mechanism by which arbitrary strings may be
generated. This mechanism is similar to filename expansion (see
Filename Expansion), but the filenames generated need not exist.
Patterns to be brace expanded take the form of an optional preamble,
followed by either a series of comma-separated strings or a sequence
expression between a pair of braces, followed by an optional
postscript. The preamble is prefixed to each string contained within
the braces, and the postscript is then appended to each resulting
string, expanding left to right.
According to the comment by @mosvy, this first appeared from csh but the behavior in bash is different from csh and other shells. This type of braces expansion is also present in glob(3).
There is another type of braces expansion a..z that only appeared after bash 3.0, and there are more added in bash 4.0.
In a shell where globbing is turned on, execute in a empty folder, the following result is returned
$ echo a[bc]d
a[bc]d
$ echo ab,cd
abd acd
In response to @Jesse_b's comment, if you are in an interactive shell and both of them applies, a[bc]d is less trouble typing. For example grep pattern [ab][12].txt.
a[bc]d is pattern-matching, and is part of the POSIX standard. In POSIX, this is introduced as the "pattern bracket expression". It is documented in section 2.13 of the manual
When unquoted and outside a bracket expression, the following three
characters shall have special meaning in the specification of
patterns:
?
A question-mark is a pattern that shall match any character.
*
An asterisk is a pattern that shall match multiple characters, as
described in Patterns Matching Multiple Characters.
[
The open bracket shall introduce a pattern bracket expression.
Section 2.13.3 also mentions something that it behaves differently from what one would expect for usual regexs when it is used for filename expansion (emphasis by me)
The rules described so far in Patterns Matching a Single Character and
Patterns Matching Multiple Characters are qualified by the following
rules that apply when pattern matching notation is used for filename
expansion:
The slash character in a pathname shall be explicitly matched by using
one or more slashes in the pattern; it shall neither be matched by the
asterisk or question-mark special characters nor by a bracket
expression. Slashes in the pattern shall be identified before bracket
expressions; thus, a slash cannot be included in a pattern bracket
expression used for filename expansion. If a slash character is found
following an unescaped open square bracket character before a
corresponding closing square bracket is found, the open bracket shall
be treated as an ordinary character. For example, the pattern
"a[b/c]d"does not match such pathnames asabdora/d.
It only matches a pathname of literallya[b/c]d.
ab,cd is braces expansion, it is not in the specification by POSIX. Here is the corresponding part from the bash manual (emphasis by me):
Brace expansion is a mechanism by which arbitrary strings may be
generated. This mechanism is similar to filename expansion (see
Filename Expansion), but the filenames generated need not exist.
Patterns to be brace expanded take the form of an optional preamble,
followed by either a series of comma-separated strings or a sequence
expression between a pair of braces, followed by an optional
postscript. The preamble is prefixed to each string contained within
the braces, and the postscript is then appended to each resulting
string, expanding left to right.
According to the comment by @mosvy, this first appeared from csh but the behavior in bash is different from csh and other shells. This type of braces expansion is also present in glob(3).
There is another type of braces expansion a..z that only appeared after bash 3.0, and there are more added in bash 4.0.
In a shell where globbing is turned on, execute in a empty folder, the following result is returned
$ echo a[bc]d
a[bc]d
$ echo ab,cd
abd acd
In response to @Jesse_b's comment, if you are in an interactive shell and both of them applies, a[bc]d is less trouble typing. For example grep pattern [ab][12].txt.
edited 8 hours ago
G-Man
13.9k93870
13.9k93870
answered 18 hours ago
Weijun ZhouWeijun Zhou
1,842429
1,842429
1
The brace expansion is not a "bashism"; it first appeared incsh, long beforebash. It's also present in the glob(3) library function. The difference is that inbashit's performed before other expansions:a=A; ab=A/B; ac=A/C; echo $ab,cwill work in bash differently from any other shell.
– mosvy
15 hours ago
Thank you. I will update the answer.
– Weijun Zhou
15 hours ago
add a comment |
1
The brace expansion is not a "bashism"; it first appeared incsh, long beforebash. It's also present in the glob(3) library function. The difference is that inbashit's performed before other expansions:a=A; ab=A/B; ac=A/C; echo $ab,cwill work in bash differently from any other shell.
– mosvy
15 hours ago
Thank you. I will update the answer.
– Weijun Zhou
15 hours ago
1
1
The brace expansion is not a "bashism"; it first appeared in
csh, long before bash. It's also present in the glob(3) library function. The difference is that in bash it's performed before other expansions: a=A; ab=A/B; ac=A/C; echo $ab,c will work in bash differently from any other shell.– mosvy
15 hours ago
The brace expansion is not a "bashism"; it first appeared in
csh, long before bash. It's also present in the glob(3) library function. The difference is that in bash it's performed before other expansions: a=A; ab=A/B; ac=A/C; echo $ab,c will work in bash differently from any other shell.– mosvy
15 hours ago
Thank you. I will update the answer.
– Weijun Zhou
15 hours ago
Thank you. I will update the answer.
– Weijun Zhou
15 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f515895%2fwhat-is-the-difference-between-abcd-brackets-and-ab-cd-braces%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
Who told you to use
command a[bc]d?– Jesse_b
18 hours ago
2
It certainly has its uses if one understands it correctly.
– Weijun Zhou
18 hours ago
2
I guess I just don't understand how the confusion between the two happened.
– Jesse_b
17 hours ago
I have been explicitly asked by a coworker less familiar with Linux on this, although not recently.
– Weijun Zhou
17 hours ago