[英]Bash:Single Quotes and Double Quotes and Exclamation Mark
我有一個簡單的腳本,名為example
:
#!/bin/sh
echo $'${1}'
請注意,這里使用$''
將\\n
轉換為新行。 ${1}
是傳遞給此Shell腳本的第一個參數。
我想將參數傳遞給此腳本example
,並顯示以下內容:
#1. You're smart!
#2. It's a difficult question!
我嘗試了以下方法:
example "#1. You're smart!\n#2. It's a difficult question!"
錯誤: -bash: !\\n#2.: event not found
然后我試圖逃脫!
用單引號,並嘗試:
example '#1. You're smart\!\n#2. It's a difficult question\!'
它輸出:
${1}
有什么解決辦法嗎? 非常感謝!
$ cat t.sh
#! /bin/bash
echo -e $@
或者echo -e $1
,或者echo -e ${1}
如果只想處理第一個參數。
要讓bash停止嘗試擴展!
,請使用set +H
(請參閱bash中的如何逃避感嘆號? )
$ set +H
$ ./t.sh "#1. You're smart!\n#2. It's a difficult question!"
#1. You're smart!
#2. It's a difficult question!
$''
表達式中的內容必須是文字。 您不能在其中展開其他變量。
但是您可以這樣做:
echo "${1//\\n/$'\n'}"
Jan Hudec有一個更好的答案:
echo -e "$1"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.