意外的字符串逃跑过程的argv [英] Unexpected strings escape in process argv
问题描述
得到还挺惊讶的:
$节点-pprocess.argv'$ SHELL$ SHELL'\\ T'\\ T''\\\\ T'
['节点','/斌/ bash的','$ SHELL','T','\\\\ T','\\\\\\\\ T']$蟒蛇-c'进口SYS;打印sys.argv中'$ SHELL$ SHELL'\\ T'\\ T''\\\\ T'
['-c','/斌/ bash的','$ SHELL','T','\\\\ T','\\\\\\\\ T']
预期相同的行为与
$回声$ SHELL$ SHELL'\\ T'\\ T''\\\\ T'
/斌/ bash的$ SHELL牛逼\\ t \\\\Ť
这就是我需要的东东被传入。
为什么多余的逃生与'\\ T'
,'\\\\ T'
过程的argv?为什么不是'$ SHELL
处理方式不同?哪来的这实际上是从哪里来的?为什么不同于回声
行为?
首先,我认为这是在 minimist 某些部分的演员的,但随后得到了相同的既裸露的Node.js和Python。可能缺少明显的东西在这里。
使用 $...
的形式来传递逃避像 \\序列ŧ
, \\ n
, \\ r
, \\ 0
等在BASH:
蟒蛇-c'进口SYS;打印sys.argv中'$ SHELL$ SHELL'\\ T $'\\ t$\\\\ T'
['-c','/斌/ bash的','$ SHELL','T','\\ T','\\\\ T']
按男人庆典
:
形式的字
$字符串
被特殊处理。该扩展为字符串,由ANSI C标准中规定的取代反斜杠转义字符。反斜杠转义序列,如果present,是德codeD如下:
块引用>\\ A警报(钟)
\\ b退格
\\ E
\\ E转义字符
\\ f换
\\ n新行
\\ r回车
\\ t水平制表
\\ v垂直制表
\\\\反斜杠
\\'单引号
\\双引号
\\ NNN 8位字符,其值是八进制值NNN(一至三位)
\\ XHH 8位字符,它的值是十六进制值HH(一到两个十六进制数字)
\\ Joey:嗯单向code(ISO / IEC 10646)字符,它的值是十六进制值HHHH(一到四十六进制数字)
\\ UHHHHHHHH单向code(ISO / IEC 10646)字符,它的值是十六进制值HHHHHHHH(一至八十六进制数字)
\\ CX控制-X字符Got kinda surprised with:
$ node -p 'process.argv' $SHELL '$SHELL' \t '\t' '\\t' [ 'node', '/bin/bash', '$SHELL', 't', '\\t', '\\\\t' ] $ python -c 'import sys; print sys.argv' $SHELL '$SHELL' \t '\t' '\\t' ['-c', '/bin/bash', '$SHELL', 't', '\\t', '\\\\t']
Expected the same behavior as with:
$ echo $SHELL '$SHELL' \t '\t' '\\t' /bin/bash $SHELL t \t \\t
Which is how I need the stuff to be passed in.
Why the extra escape with
'\t'
,'\\t'
in process argv? Why handled differently than'$SHELL'
? Where's this actually coming from? Why different from theecho
behavior?First I thought this to be some extras on the minimist part, but then got the same with both bare Node.js and Python. Might be missing something obvious here.
解决方案Use
$'...'
form to pass escape sequences like\t
,\n
,\r
,\0
etc in BASH:python -c 'import sys; print sys.argv' $SHELL '$SHELL' \t $'\t' $'\\t' ['-c', '/bin/bash', '$SHELL', 't', '\t', '\\t']
As per
man bash
:Words of the form
$'string'
are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
\a alert (bell) \b backspace \e \E an escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \' single quote \" double quote \nnn the eight-bit character whose value is the octal value nnn (one to three digits) \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) \uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits) \UHHHHHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits) \cx a control-x character
这篇关于意外的字符串逃跑过程的argv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!