击:单引号和双引号和感叹号 [英] Bash:Single Quotes and Double Quotes and Exclamation Mark

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

问题描述

我有一个名为一个简单的脚本例如

 #!/ bin / sh的
回声$$ {1}

请注意, $的使用''这里是转换 \\ n 成新行。
$ {1} 是传递给这个shell脚本的第一个参数。

我要一个参数传递给此脚本例如和它打印以下内容:

 #1。你真聪明!
#2。这是一个很难回答的问题!

我试过如下:

 例如#1。你很聪明!\\ N#2,这是一个很难回答的问题!

错误: -bash:\\没有找到N#2:事件

然后我试图逃跑 按单引号,并试图!

 例如#1。你真聪明!\\ \\ N#2。这是一个很难回答的问题!\\

它输出:

  $ {1}

在这里的任何解决方案?非常感谢!


解决方案

  $猫t.sh
#! /斌/庆典
回声-e $ @

回声-e $ 1 回声-e $ {1} 如果你只是想处理第一个参数。

要获得的bash停止试图展开,使用设置+ H (见的In bash中,我怎么逃感叹号?

  $组+ H
$ ./t.sh#1。你真聪明!\\ N#2,这是一个很难回答的问题!
#1。你真聪明!
#2。这是一个很难回答的问题!

I have a simple script named example:

#!/bin/sh
echo $'${1}'

Please note that the usage of $'' here is to convert \n into new line. ${1} is the first parameter passed to this shell script.

I want to pass a parameter to this script example and it prints the following:

#1. You're smart!
#2. It's a difficult question!

I tried the following:

example "#1. You're smart!\n#2. It's a difficult question!"

An error: -bash: !\n#2.: event not found

Then I tried to escape ! by single quote, and tried:

example '#1. You're smart\!\n#2. It's a difficult question\!'

It outputs:

${1}

Any solution here? Thanks a lot!

解决方案

$ cat t.sh
#! /bin/bash
echo -e $@

Or echo -e $1, or echo -e ${1} if you just want to process the first argument.

To get bash to stop trying to expand !, use set +H (see In bash, how do I escape an exclamation mark?)

$ 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!

这篇关于击:单引号和双引号和感叹号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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