哪些角色需要在Bash中转义?我们怎么知道? [英] Which characters need to be escaped in Bash? How do we know it?

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

问题描述

是否有任何需要在Bash中转义的字符的完整列表?可以用 sed 检查吗?



特别是,我检查是否需要进行转义。我试过

  echoh%h| sed's /%/ i / g'

并且正常工作,不需要转义。是否意味着不需要转义?这是检查必要性的好方法吗?



更一般的说法是:他们在 shell中转义相同的字符 bash

解决方案

有两个简单而安全的规则不仅在 sh ,而且 bash



1。将整个字符串放在单引号



这适用于除单引号外的所有字符。为了避免单引号,请先关闭引号,插入单引号,重新打开引号。

 '我'\''mas @ fe $ tring,以换行符结尾
'

sed命令: sed -es /'/'\\\\'/ g; 1s / ^ /'/; \ $ s / \ $ /'/ code>



2。使用反斜杠逃避每个字符



这适用于除换行符之外的所有字符。对于换行符,使用单引号或双引号。 空字符串仍然需要处理 - 替换为

 code> \I\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \g\ \w\h\i\c\h\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \w\l\i\\\
\e

sed命令: sed -e的/./ \\& / g; 1 {$ S / ^ $ / /}; 1!s / ^ //; $!s / $ //'



2b。更可读的版本2



有一个易于安全的字符集,如 [a-zA-Z0-9,._ +:@% / - ] ,可以保持未转义,以使其更易读。

 我不\ a\ s @ fe \ \ $ tring\ which\ ends\ in\ newline

sed命令: LC_ALL = C sed -e's / [^ a-zA-Z0-9,._ + @%/ - ] / \ \&安培/克; 1 {$ S / ^ $ / /};






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



请注意,shell变量仅针对POSIX意义上的文本定义。未定义处理二进制数据。对于重要的实现,二进制工作除了NUL字节(因为变量用C字符串实现,意图用作C字符串,即程序参数),但是您应该切换到binary区域设置,如latin1






(您可以通过阅读 sh 。对于bash,请查看由@AustinPhillips链接的参考手册)


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?

解决方案

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

1. Put the whole string in single quotes

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 command: sed -e "s/'/'\\\\''/g; 1s/^/'/; \$s/\$/'/"

2. Escape every char with a backslash

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 command: sed -e 's/./\\&/g; 1{$s/^$/""/}; 1!s/^/"/; $!s/$/"/'.

2b. More readable version of 2

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 command: LC_ALL=C sed -e 's/[^a-zA-Z0-9,._+@%/-]/\\&/g; 1{$s/^$/""/}; 1!s/^/"/; $!s/$/"/'.


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.

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.


(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天全站免登陆