gpt4 book ai didi

bash - 'Brace expansion' 在 Bash 中如何工作?

转载 作者:行者123 更新时间:2023-11-29 09:48:09 25 4
gpt4 key购买 nike

http://www.tldp.org/LDP/abs/html/special-chars.html 中有一个示例

$ echo {file1,file2}\ :{\ A," B",' C'}  
file1 : A file1 : B file1 : C file2 : A file2 : B file2 : C

这是如何运作的?
我像下面这样更改此命令:

$ echo {file1,file2}\  {\ A," B",' C'}  
file1 file2 A B C
$ echo {file1,file2}\ -{\ A," B",' C'}
file1 - A file1 - B file1 - C file2 - A file2 - B file2 - C

为什么第一个命令不像示例中的命令那样工作?
帮助!

我知道我引入了一个额外的空间。如果将额外的空格更改为其他内容(冒号、破折号等),那么一切都很好。
没有关于命令的详细信息,例如: echo {"A","B", ' C'}\{"D","E", ' F'}\{"G","H", ' I'} in http://www.tldp.org/LDP/abs/html/special-chars.html .我想知道更多。

最佳答案

Why the first command does not work like the command in the example?

因为你引入了一个额外的空间:

#                     |==> This is it!
# |
$ echo {file1,file2}\ {\ A," B",' C'}
file1 file2 A B C
$ echo {file1,file2}\ {\ A," B",' C'} # This is probably what you expected!
file1 A file1 B file1 C file2 A file2 B file2 C

额外的空间导致 shell 首先展开 {file1,file2} 然后 {\A,"B",' C'}


引自Brace Expansion :

Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a seqeunce 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.

当你在大括号扩展后引入一个空格时(一个 Backspace 后跟 两个 空格是一个空格!),下面的记号不再被认为是一个 postscript 并单独展开。

关于bash - 'Brace expansion' 在 Bash 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20943574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com