这是GNU Bash中的错误吗?(Is this a bug in GNU Bash?)

我试图支持字符串安全性的脚本时发现了这一点:

$ echo '!!' !! $ echo "$(echo '!!')" echo "$(echo 'echo '!!'')" #<~ the console echoes the line with expanded history here echo !! #<~ the result

在我看来,最内层的引用,单引号,不应该扩展任何东西 ,变量,子shell或其他,但在这种情况下,它扩展了!! 到最后一行输入。 似乎不应该这样做。

我问你:这是Bash中的一个错误,如果可以使用引用的子shell扩展输出感叹号?

(在Linux中使用Bash 4.1.007)

编辑:

如果以上不是错误,那么为什么这样做会如预期的那样?

$ foo='some value' $ echo "$(echo 'neither $foo nor `this subshell` should expand here')" neither $foo nor `this subshell` should expand here

I found this out while attempting to shore up my scripts for string security:

$ echo '!!' !! $ echo "$(echo '!!')" echo "$(echo 'echo '!!'')" #<~ the console echoes the line with expanded history here echo !! #<~ the result

It seems to me that the innermost quoting, which is single-quoted, should not expand anything, variable, subshell, or otherwise, but in this case it expands the !! to the last line typed. Seems like it shouldn't do that.

I ask you: is this a bug in Bash, and if it is possible to use a quoted subshell expansion that outputs an exclamation mark?

(Using Bash 4.1.007 in Linux)

Edit:

If the above isn't a bug, why, then, does this behave as expected?

$ foo='some value' $ echo "$(echo 'neither $foo nor `this subshell` should expand here')" neither $foo nor `this subshell` should expand here

最满意答案

我同意。

$ echo "$(echo '!!')" echo "$(echo 'echo $(echo '!!')')" echo $(echo !!)

应该做同样的事情

$ echo $(echo '!!') !!

我无法看到如何根据历史扩展文档解释差异。

历史扩展文档与其他shell扩展文档完全分离也很奇怪。

zsh回声!! 对于两者,至少我的设置。

I agree.

$ echo "$(echo '!!')" echo "$(echo 'echo $(echo '!!')')" echo $(echo !!)

should do the same as

$ echo $(echo '!!') !!

I can't see how to explain the difference based on the history expansion documentation.

It's also odd how the history expansion docs are completely separate from the rest of the shell expansions documentation.

zsh echoes !! for both, at least with my setup.

这是GNU Bash中的错误吗?(Is this a bug in GNU Bash?)

我试图支持字符串安全性的脚本时发现了这一点:

$ echo '!!' !! $ echo "$(echo '!!')" echo "$(echo 'echo '!!'')" #<~ the console echoes the line with expanded history here echo !! #<~ the result

在我看来,最内层的引用,单引号,不应该扩展任何东西 ,变量,子shell或其他,但在这种情况下,它扩展了!! 到最后一行输入。 似乎不应该这样做。

我问你:这是Bash中的一个错误,如果可以使用引用的子shell扩展输出感叹号?

(在Linux中使用Bash 4.1.007)

编辑:

如果以上不是错误,那么为什么这样做会如预期的那样?

$ foo='some value' $ echo "$(echo 'neither $foo nor `this subshell` should expand here')" neither $foo nor `this subshell` should expand here

I found this out while attempting to shore up my scripts for string security:

$ echo '!!' !! $ echo "$(echo '!!')" echo "$(echo 'echo '!!'')" #<~ the console echoes the line with expanded history here echo !! #<~ the result

It seems to me that the innermost quoting, which is single-quoted, should not expand anything, variable, subshell, or otherwise, but in this case it expands the !! to the last line typed. Seems like it shouldn't do that.

I ask you: is this a bug in Bash, and if it is possible to use a quoted subshell expansion that outputs an exclamation mark?

(Using Bash 4.1.007 in Linux)

Edit:

If the above isn't a bug, why, then, does this behave as expected?

$ foo='some value' $ echo "$(echo 'neither $foo nor `this subshell` should expand here')" neither $foo nor `this subshell` should expand here

最满意答案

我同意。

$ echo "$(echo '!!')" echo "$(echo 'echo $(echo '!!')')" echo $(echo !!)

应该做同样的事情

$ echo $(echo '!!') !!

我无法看到如何根据历史扩展文档解释差异。

历史扩展文档与其他shell扩展文档完全分离也很奇怪。

zsh回声!! 对于两者,至少我的设置。

I agree.

$ echo "$(echo '!!')" echo "$(echo 'echo $(echo '!!')')" echo $(echo !!)

should do the same as

$ echo $(echo '!!') !!

I can't see how to explain the difference based on the history expansion documentation.

It's also odd how the history expansion docs are completely separate from the rest of the shell expansions documentation.

zsh echoes !! for both, at least with my setup.