[英]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.