且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决错误“bash: !d': event not found"在 Bash 命令替换中

更新时间:2021-10-10 05:31:39

问题在于,在双引号内,bash 试图在将 !d 传递给子shell 之前对其进行扩展.您可以通过删除双引号来解决这个问题,但我也建议简化您的脚本:

The problem is that within double quotes, bash is trying to expand !d before passing it to the subshell. You can get around this problem by removing the double quotes but I would also propose a simplification to your script:

VNCServerAndDisplayNumber=$(echo "$VNCServerResponse" | awk '/desktop/ {print $NF}')

这只是打印包含桌面"一词的行上的最后一个字段.

This simply prints the last field on the line containing the word "desktop".

在较新的 bash 上,您可以使用 herestring 而不是管道 echo:

On a newer bash, you can use a herestring rather than piping an echo:

VNCServerAndDisplayNumber=$(awk '/desktop/ {print $NF}' <<<"$VNCServerResponse")