在Bash中如何解释具有多种引号的字符串?
为了给出上下文,我正在尝试创建一个简单的bash版本,为此,我需要模仿用多组单一和双引号的bash解析内容的方式。我无法弄清Bash在引号中处理引号的整体过程。我注意到一些重复的模式,但仍然没有完整的图片。
例如,此示例:
$ "'"ls"'"
评估为:
$ 'ls'
甚至更丑陋的示例:
$ "'"'"""'"ls"'"""'"'"
评估对:
$ '"""ls"""'
我注意到以下模式出现:
- 如果包装报价计数甚至是对逆报价中的内部评估,
- 则包装报价计数是奇怪的,包含反向引号内的内容。
例如,甚至包装引号:
$ ""'ls'""
评估反向引号(单引号),而没有单引号本身,评估:
$ ls
或包装器的奇数计数:
$ '''"ls"'''
它评估对双引号的内容,包括:
$ "ls" : command not found.
我仍然没有完整的内容在引号中如何完成这种复杂引号的解析模式的图片。
To give context, I'm trying to create a simple version of bash, and for that I need to mimic the way bash parses content with multiple sets of single and double quotes. I can't figure out the overall procedure by which bash handles quotes inside quotes. I noticed some repeated patterns but still don't have the full picture.
For instance this example:
$ "'"ls"'"
evaluates to:
$ 'ls'
or even this uglier example:
$ "'"'"""'"ls"'"""'"'"
evaluates to:
$ '"""ls"""'
I noticed the following patterns arise:
- if count of wrapping quotes are even it evaluates to what's inside the inverse quotes exclusively
- if count of wrapping quotes are odd it evaluates to what's inside the inverse quotes inclusively.
For example even wrapping quotes:
$ ""'ls'""
evaluates to what's inside the inverse quotes (single quotes) without the single quotes themselves, evaluation:
$ ls
Or for odd count of wrapper quotes:
$ '''"ls"'''
it evaluates to content of double quotes inclusively:
$ "ls" : command not found.
Still I don't get the full picture of how this parsing pattern for more complex quotes inside quotes is done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引号是顺序处理的,寻找匹配的截止报价。开始引用和结束引用之间的所有内容都变成了一个字符串。嵌套的引号没有特殊的含义。
当它处理第一个
“
>)时,它会扫描查找结束字符串的下一个“
”,其中包含'
。然后它扫描固定的字符串
ls
。当它处理
“
ls
之后)时,它会扫描寻找下一个“
”,从而导致另一个字符串'
。这些都是串联的,导致
'ls'
。“'”
是字符串'
。'“”“”'
是字符串“”“
” ls“
是字符串ls
' “”''
是字符串“”“
”''
是字符串'
。将它们串联在一起产生
'“”“ LS”“”'
“”
是一个空字符串。'ls'
是字符串ls
。“”
是另一个空字符串。将它们串在一起产生ls
。''
是一个空字符串。'“ ls”'
是字符串“ ls”
(包含字面的双引号)。''
是一个空字符串。串联它们产生“ LS”
。由于该名称没有命令(包括字面的双引号),因此您会遇到错误。单引号和双引号之间存在差异,但它们不会影响您发布的任何示例。请参阅 bash 中单引号和双引号之间的差异
Quotes are processed sequentially, looking for matching closing quotes. Everything between the starting and ending quote becomes a single string. Nested quotes have no special meaning.
When it processes the first
"
, it scans looking for the next"
that ends the string, which contains'
.Then it scans the fixed string
ls
.When it processes the
"
afterls
, it scans looking for the next"
, resulting in another string'
.These are all concatenated, resulting in
'ls'
."'"
is the string'
.'"""'
is the string"""
"ls"
is the stringls
'"""'
is the string"""
"'"
is the string'
.Concatenating them all together produces
'"""ls"""'
""
is an empty string.'ls'
is the stringls
.""
is another empty string. Concatenating them together producesls
.''
is an empty string.'"ls"'
is the string"ls"
(containing literal double quotes).''
is an empty string. Concatenating them produces"ls"
. Since there's no command with that name (including the literal double quotes), you get an error.There are differences between single and double quotes, but they don't affect any of the examples you posted. See Difference between single and double quotes in Bash