在Bash中是否有需要转义的字符的综合列表?仅仅用sed可以检查吗?

特别是,我正在检查%是否需要转义。我试着

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

并工作良好,没有逃脱%。这是否意味着%不需要转义?这是检查必要性的好方法吗?

更一般的是:它们是shell和bash中转义的相同字符吗?


当前回答

使用打印'%q'技术,我们可以运行一个循环来找出哪些字符是特殊的:

#!/bin/bash
special=$'`!@#$%^&*()-_+={}|[]\\;\':",.<>?/ '
for ((i=0; i < ${#special}; i++)); do
    char="${special:i:1}"
    printf -v q_char '%q' "$char"
    if [[ "$char" != "$q_char" ]]; then
        printf 'Yes - character %s needs to be escaped\n' "$char"
    else
        printf 'No - character %s does not need to be escaped\n' "$char"
    fi
done | sort

它给出如下输出:

No, character % does not need to be escaped
No, character + does not need to be escaped
No, character - does not need to be escaped
No, character . does not need to be escaped
No, character / does not need to be escaped
No, character : does not need to be escaped
No, character = does not need to be escaped
No, character @ does not need to be escaped
No, character _ does not need to be escaped
Yes, character   needs to be escaped
Yes, character ! needs to be escaped
Yes, character " needs to be escaped
Yes, character # needs to be escaped
Yes, character $ needs to be escaped
Yes, character & needs to be escaped
Yes, character ' needs to be escaped
Yes, character ( needs to be escaped
Yes, character ) needs to be escaped
Yes, character * needs to be escaped
Yes, character , needs to be escaped
Yes, character ; needs to be escaped
Yes, character < needs to be escaped
Yes, character > needs to be escaped
Yes, character ? needs to be escaped
Yes, character [ needs to be escaped
Yes, character \ needs to be escaped
Yes, character ] needs to be escaped
Yes, character ^ needs to be escaped
Yes, character ` needs to be escaped
Yes, character { needs to be escaped
Yes, character | needs to be escaped
Yes, character } needs to be escaped

有些结果,看起来有点可疑。获得@CharlesDuffy在这方面的投入会很有趣。

其他回答

有两个简单而安全的规则,它们不仅可以在sh中工作,还可以在bash中工作。

1. 把整个字符串放在单引号中

这适用于所有字符,除了单引号本身。若要转义单引号,请关闭它前面的引号,插入单引号,然后重新打开引号。

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

Sed命令:Sed -e "s/'/'\\\\ " /g;1年代/ ^ / /;\ s / \ /美元/”

2. 用反斜杠转义每个char

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

\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 's/./\\&/g;1 {$ s / ^ $ / "};1 ! s / ^ /”;美元! s / /美元“/”。

2 b。更可读的版本2

有一组简单安全的字符,比如[a-zA-Z0-9,]。_+:@%/-],可以不转义,以使其更具可读性

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

sed命令:LC_ALL = C sed - e ' s / [^ a-zA-Z0-9 ,._+@%/-]/\\&/ g;1 {$ s / ^ $ / "};1 ! s / ^ /”;美元! s / /美元“/”。


注意,在sed程序中,不能知道输入的最后一行是否以换行符字节结束(除非换行符为空)。这就是为什么上面的sed命令都假设它不存在。您可以手动添加带引号的换行符。

注意,外壳变量仅为POSIX意义上的文本定义。没有定义如何处理二进制数据。对于重要的实现,二进制除了NUL字节(因为变量是用C字符串实现的,意味着被用作C字符串,即程序参数),但您应该切换到“二进制”语言环境,例如latin1。


(你可以通过阅读sh的POSIX规范来验证规则。对于bash,查看@AustinPhillips链接的参考手册)

在Bourne或POSIX shell中需要转义的角色与Bash不同。通常(very) Bash是这些shell的超集,所以在shell中转义的任何内容都应该在Bash中转义。

一个很好的一般规则是“如果有疑问,逃避它”。但是转义某些字符会赋予它们特殊的含义,比如\n。这些在引用和回声下的man bash页面中列出。

除此之外,转义任何非字母数字的字符更安全。我不知道一个明确的清单。

手册页在某个地方列出了它们,但不是在一个地方。学习语言,那是必胜之道。

其中一个让我大吃一惊的是!这是Bash(和csh)中的一个特殊字符(历史扩展),但在Korn shell中不是。即使是回音“Hello world!”也会出问题。像往常一样,使用单引号就去掉了特殊含义。

可以作为shell输入重用的格式

编辑2021年2月:bash ${var@Q}

在bash下,你可以使用参数扩展的@命令来存储变量内容,用于参数转换:

${parameter@operator} Parameter transformation. The expansion is either a transforma‐ tion of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter: Q The expansion is a string that is the value of parameter quoted in a format that can be reused as input. ... A The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value.

示例:

$ var=$'Hello\nGood world.\n'
$ echo "$var"
Hello
Good world.

$ echo "${var@Q}"
$'Hello\nGood world.\n'

$ echo "${var@A}"
var=$'Hello\nGood world.\n'

旧的答案

有一个特殊的printf格式指令(%q)用于这种请求:

Printf [-v var] format[参数] %q导致printf输出相应的参数 可以作为shell输入重用的格式。

一些样品:

read foo
Hello world
printf "%q\n" "$foo"
Hello\ world

printf "%q\n" $'Hello world!\n'
$'Hello world!\n'

这也可以通过变量来使用:

printf -v var "%q" "$foo
"
echo "$var"
$'Hello world\n'

快速检查所有(128)ascii字节:

注意,从128到255的所有字节都必须转义。

for i in {0..127} ;do
    printf -v var \\%o $i
    printf -v var $var
    printf -v res "%q" "$var"
    esc=E
    [ "$var" = "$res" ] && esc=-
    printf "%02X %s %-7s\n" $i $esc "$res"
done |
    column

这必须呈现如下内容:

00 E ''         1A E $'\032'    34 - 4          4E - N          68 - h      
01 E $'\001'    1B E $'\E'      35 - 5          4F - O          69 - i      
02 E $'\002'    1C E $'\034'    36 - 6          50 - P          6A - j      
03 E $'\003'    1D E $'\035'    37 - 7          51 - Q          6B - k      
04 E $'\004'    1E E $'\036'    38 - 8          52 - R          6C - l      
05 E $'\005'    1F E $'\037'    39 - 9          53 - S          6D - m      
06 E $'\006'    20 E \          3A - :          54 - T          6E - n      
07 E $'\a'      21 E \!         3B E \;         55 - U          6F - o      
08 E $'\b'      22 E \"         3C E \<         56 - V          70 - p      
09 E $'\t'      23 E \#         3D - =          57 - W          71 - q      
0A E $'\n'      24 E \$         3E E \>         58 - X          72 - r      
0B E $'\v'      25 - %          3F E \?         59 - Y          73 - s      
0C E $'\f'      26 E \&         40 - @          5A - Z          74 - t      
0D E $'\r'      27 E \'         41 - A          5B E \[         75 - u      
0E E $'\016'    28 E \(         42 - B          5C E \\         76 - v      
0F E $'\017'    29 E \)         43 - C          5D E \]         77 - w      
10 E $'\020'    2A E \*         44 - D          5E E \^         78 - x      
11 E $'\021'    2B - +          45 - E          5F - _          79 - y      
12 E $'\022'    2C E \,         46 - F          60 E \`         7A - z      
13 E $'\023'    2D - -          47 - G          61 - a          7B E \{     
14 E $'\024'    2E - .          48 - H          62 - b          7C E \|     
15 E $'\025'    2F - /          49 - I          63 - c          7D E \}     
16 E $'\026'    30 - 0          4A - J          64 - d          7E E \~     
17 E $'\027'    31 - 1          4B - K          65 - e          7F E $'\177'
18 E $'\030'    32 - 2          4C - L          66 - f      
19 E $'\031'    33 - 3          4D - M          67 - g      

其中第一个字段是字节的十六进制值,第二个字段包含E,如果字符需要转义,第三个字段显示字符的转义表示。

为什么,?

您可能会看到一些不总是需要转义的字符,如,,}和{。

所以不是总是,而是有时:

echo test 1, 2, 3 and 4,5.
test 1, 2, 3 and 4,5.

or

echo test { 1, 2, 3 }
test { 1, 2, 3 }

但医改:

echo test{1,2,3}
test1 test2 test3

echo test\ {1,2,3}
test 1 test 2 test 3

echo test\ {\ 1,\ 2,\ 3\ }
test  1 test  2 test  3

echo test\ {\ 1\,\ 2,\ 3\ }
test  1, 2 test  3 

使用打印'%q'技术,我们可以运行一个循环来找出哪些字符是特殊的:

#!/bin/bash
special=$'`!@#$%^&*()-_+={}|[]\\;\':",.<>?/ '
for ((i=0; i < ${#special}; i++)); do
    char="${special:i:1}"
    printf -v q_char '%q' "$char"
    if [[ "$char" != "$q_char" ]]; then
        printf 'Yes - character %s needs to be escaped\n' "$char"
    else
        printf 'No - character %s does not need to be escaped\n' "$char"
    fi
done | sort

它给出如下输出:

No, character % does not need to be escaped
No, character + does not need to be escaped
No, character - does not need to be escaped
No, character . does not need to be escaped
No, character / does not need to be escaped
No, character : does not need to be escaped
No, character = does not need to be escaped
No, character @ does not need to be escaped
No, character _ does not need to be escaped
Yes, character   needs to be escaped
Yes, character ! needs to be escaped
Yes, character " needs to be escaped
Yes, character # needs to be escaped
Yes, character $ needs to be escaped
Yes, character & needs to be escaped
Yes, character ' needs to be escaped
Yes, character ( needs to be escaped
Yes, character ) needs to be escaped
Yes, character * needs to be escaped
Yes, character , needs to be escaped
Yes, character ; needs to be escaped
Yes, character < needs to be escaped
Yes, character > needs to be escaped
Yes, character ? needs to be escaped
Yes, character [ needs to be escaped
Yes, character \ needs to be escaped
Yes, character ] needs to be escaped
Yes, character ^ needs to be escaped
Yes, character ` needs to be escaped
Yes, character { needs to be escaped
Yes, character | needs to be escaped
Yes, character } needs to be escaped

有些结果,看起来有点可疑。获得@CharlesDuffy在这方面的投入会很有趣。

为了不让别人不得不RTFM…在bash中:

用双引号括起来的字符会保留引号内所有字符的文字值,除了$、'、\和(当启用历史扩展时)!

...所以如果你转义了这些(当然还有引用本身),你可能就没事了。

如果你采取更保守的“当有疑问时,转义它”的方法,应该可以通过不转义标识符字符(即ASCII字母、数字或“_”)来避免获得具有特殊含义的字符。这是非常不可能的(即在一些奇怪的posix shell中)有特殊的含义,因此需要转义。