bash脚本中的双重感叹号 [英] Double exclamation in bash script

查看:56
本文介绍了bash脚本中的双重感叹号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在打印双感叹号时,它会执行上一条命令.但是回声!给出了一些我不理解的奇怪结果.例如,当在bash脚本中在命令下方键入时,它也会在输出中打印回显

I know when double exclamation is printed, it executes the previous command. But echo !! gives some strange results which I don't understand. For example when typed below command in bash script, it prints echo too as part of the output

echo $$
echo !!
This prints the below output:
echo echo $$
echo 3150
(Why does echo precede every output ?)

推荐答案

使用历史记录替换时,shell首先显示要执行的命令,并显示所有替换,然后执行该命令.这样一来,您就可以查看生成的命令是什么,以确认它是您期望的.

When you use history substitution, the shell first displays the command that it's about to execute with all the substitutions shown, and then executes it. This is so you can see what the resulting command is, to confirm that it's what you expected.

因此,如果您输入:

some command
echo !!

!! 替换为上一个命令的内容.因此它显示然后执行

the !! is replaced with the contents of the previous command. So it displays and then executes

echo some command

这篇关于bash脚本中的双重感叹号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆