其中人物需要的Bash转义?我们怎么知道呢? [英] Which characters need to be escaped in Bash? How do we know it?

查看:118
本文介绍了其中人物需要的Bash转义?我们怎么知道呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有需要在Bash中转义字符的任何COM prehensive列表?是否可以用 SED

Is there any comprehensive list of characters that need to be escaped in Bash? Can it be checked just with sed?

在特定的,我被检查是否需要转义或没有。我试过

In particular, I was checking whether % needs to be escaped or not. I tried

echo "h%h" | sed 's/%/i/g'

和工作正常,没有逃避。这是否意味着并不需要进行转义?这是检查的必要性的好办法?

and worked fine, without escaping %. Does it mean % does not need to be escaped? Was this a good way to check the necessity?

和更普遍的:它们是相同的字符,在逃跑庆典

And more general: are they the same characters to escape in shell and bash?

推荐答案

这帖子是老了,但我想添加这个答案,因为其他的答案是不令人满意的。

This post is old, but I'd like to add this answer because the other answers aren't satisfying.

有其中两个工作不仅在 SH 庆典

There are two easy and safe rules which work not only in sh but also bash.

1。把整个字符串中的单引号。

这适用于除单引号本身的所有字符。为了逃避单引号,关闭报价之前,插入单引号,并重新打开引用。

This works for all chars except single quote itself. To escape the single quote, close the quoting before it, insert the single quote, and re-open the quoting.

'I'\''m a s@fe $tring which ends in newline
'

sed的命令: SED -eS /'/'\\\\\\\\''/克; 1秒/ ^ /'/; \\ $ S / \\ $ /'/

2。逃生每字符用一个反斜杠

这适用于除换行符的所有字符。对于换行符使用单或双引号。的空字符串仍然必须处理 - 以

This works for all characters except newline. For newline characters use single or double quotes. Empty strings must still be handled - replace with ""

\I\'\m\ \a\ \s\@\f\e\ \$\t\r\i\n\g\ \w\h\i\c\h\ \e\n\d\s\ \i\n\ \n\e\w\l\i\n\e"
"

sed的命令:的sed -e的/./\\\\&安培/克; 1 {$ S / ^ $ //}; 1秒/ ^ /!/;!$ S / $ //

有一个简单安全的字符集,如 [A-ZA-Z0-9,._ +:@%/ - ] ,其被转义为保持它更可读

There's an easy safe set of characters, like [a-zA-Z0-9,._+:@%/-], which can be left unescaped to keep it more readable

I\'m\ a\ s@fe\ \$tring\ which\ ends\ in\ newline"
"

sed的命令: LC_ALL = C的sed -e'S / [^ A-ZA-Z0-9,._ + @%/ - / \\\\&安培/克; 1 {$ S / ^ $ //}; 1秒/ ^ /!/;!$ S / $ //

请注意,在SED程序,可以不知道输入的最后一行是否以换行符字节结束(除了当它是空的)。这就是为什么这两个以上sed命令假定它没有。您可以手动添加引用换行符。

Note that in a sed program, one can't know whether the last line of input ends with a newline byte (except when it's empty). That's why both above sed commands assume it does not. You can add a quoted newline manually.

注意,shell变量只在POSIX意义上的文字定义。处理二进制数据未被定义。对于这个问题的实现,二进制作品与NUL字节除外(因变量与C字符串实现,意味着作为C字符串,即程序参数),但你应该切换到二进制的语言环境,如LATIN1

Note that shell variables are only defined for text in the POSIX sense. Processing binary data is not defined. For the implementations that matter, binary works with the exception of NUL bytes (because variables are implemented with C strings, and meant to be used as C strings, namely program arguments), but you should switch to a "binary" locale such as latin1.

(你可以很容易地通过阅读POSIX规范为 SH 。对于bash验证规则,通过检查链接@AustinPhillips参考手册)

(You can easily validate the rules by reading the POSIX spec for sh. For bash, check the reference manual linked by @AustinPhillips)

这篇关于其中人物需要的Bash转义?我们怎么知道呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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