linux命令行中,双引号中的感叹号将被解释为历史命令
linux命令行中,雙引號中的感嘆號將被解釋為歷史命令。
命令:
test -e ttt/ && echo "file exist!" || echo "file Not exist!"
輸出:
bash: !": event not found
命令:
test -e ttt/ && echo "file exist" || echo "file Not exist"
輸出:
file exist
實驗如下:
arthur@E430 ~/workspace/test]$ls bin HelloWorld.class HelloWorld.java re t2.sh t.sh d HelloWorld.jar HelloWorld.java~ t1.sh t.py [arthur@E430 ~/workspace/test]$echo "!!" echo "ls " ls [arthur@E430 ~/workspace/test]$pwd /home/arthur/workspace/test [arthur@E430 ~/workspace/test]$echo "!l" echo "ls " ls? [arthur@E430 ~/workspace/test]$echo "!!" echo "echo "ls "" echo ls? [arthur@E430 ~/workspace/test]$echo "!!" echo "echo "echo "ls """ echo echo ls? [arthur@E430 ~/workspace/test]$
如果是想要輸出感嘆號,可以:
1. 使用單引號。
一般要輸出特殊符號,可以用單引號'引文',或者\。 這時候最好用單引號,如: echo 'Hello World !'比如要輸出That's good.就用 echo "That's good."2.?
\! 表示感嘆號 比如echo Hello\ World\ \!, linux很多符號都是用轉(zhuǎn)義符"\"來表示的,盡量不要用雙引號“”。 原因你自己 echo "Hello World !",就會發(fā)現(xiàn)輸出錯誤的, 而 echo "Hello World \!",又發(fā)現(xiàn)連“\”也一起輸出了
查資料如下:
在雙引號中,感嘆號(!)的含義根據(jù)使用的場合有所不同,在命令行環(huán)境,它將被解釋為一個歷史命令,而在腳本中,則不會有特殊含義。
Advanced Bash-Scripting Guide: 5.1. Quoting Variables 寫道 Encapsulating "!" within double quotes gives an error when used from the command line. This is interpreted as a history command. Within a script, though, this problem does not occur, since the Bash history mechanism is disabled then.?
在命令行環(huán)境,感嘆號(!)稱之為“歷史擴展字符(the? history? expansion character)”。
[root@jfht ~]#?pwd?
/root
[root@jfht ~]#?echo "!"?
-bash: !: event not found
[root@jfht ~]#?echo "!pwd"?
echo "pwd"
pwd
[root@jfht ~]#
?
在腳本中使用感嘆號,將不會進行歷史擴展。
參考資料:
http://codingstandards.iteye.com/blog/1166282
在雙引號中,感嘆號(!)的含義根據(jù)使用的場合有所不同,在命令行環(huán)境,它將被解釋為一個歷史命令,而在腳本中,則不會有特殊含義。
Advanced Bash-Scripting Guide: 5.1. Quoting Variables 寫道 Encapsulating "!" within double quotes gives an error when used from the command line. This is interpreted as a history command. Within a script, though, this problem does not occur, since the Bash history mechanism is disabled then.?
在命令行環(huán)境,感嘆號(!)稱之為“歷史擴展字符(the? history? expansion character)”。
[root@jfht ~]#?pwd?
/root
[root@jfht ~]#?echo "!"?
-bash: !: event not found
[root@jfht ~]#?echo "!pwd"?
echo "pwd"
pwd
[root@jfht ~]#
?
在腳本中使用感嘆號,將不會進行歷史擴展。
總結(jié)
以上是生活随笔為你收集整理的linux命令行中,双引号中的感叹号将被解释为历史命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 到底什么是推荐?
- 下一篇: linux下=号与==号