shell指令碼學習之echo輸出字串形式
阿新 • • 發佈:2019-01-31
echo有三種形式
輸入
$echo hello world
$echo 'hello world'
$echo "hello world"
對於上述字串,三者都可以正確輸出
hello world
三者各有特點
i 不加引號:用分號
隔開時會出問題,因為指令碼執行命令時用分號 ;
來隔開多個命令,;
後為新一次命令的執行。
輸入
$echo hello;world
輸出
hello
No command 'world' found, did you mean:
Command 'tworld' from package 'tworld' (universe)
world: command not found
ii 單引號: 將單引號中的內容原樣輸出,因此不能在其中使用表示式
輸入
$echo 'hello $?'
輸出
hello $?
iii 雙引號: !
一般情況下不能在雙引號中使用
輸入
$echo "hello !world"
輸出
bash: !world: event not found
雙引號中可以使用轉義字元來列印!
或者使用set +H
來關閉!
的功能,將其當作一個普通字元。
輸入
$echo "hello \!world"
輸出(將轉義字元也打印出來了…)
hello \!world
輸入
$set +H
$echo "hello !world"
輸出
hello !world