简体   繁体   English

Bash范围的非数字或字母

[英]Bash ranges of non digit or letters

I played around with bash {..} constructs today. 今天我玩了bash {..}构造。 I knew 我知道

{a..z}

would generate all letters, 会生成所有字母,

{0..9}

digits etc. (numbers in general obviously), but By mistake I got 数字等(一般数字显然),但我错了

{Z..a}

yielding: 收益:

Z [  ] ^ _ ` a

The characters in between "Z" (90) and "a" (97) are the ASCII 91-96. “Z”(90)和“a”(97)之间的字符是ASCII 91-96。 The astute reader will notice there is a character missing - "\\", 92. I'm guessing because of it's special nature. 精明的读者会注意到有一个角色缺失 - “\\”,92。我猜是因为它的特殊性。 Is this expected behavior as output? 这种预期行为是否作为输出? Specifically, I'm guessing the \\ is being used to escape the space in front of it after substitution, but @John1024 notes that: 具体来说,我猜测\\在替换之后被用来逃避它前面的空间,但@ John1024注意到:

echo {Z..a}a

will complain on missing backticks, while the previous version (no a ) does not. 会抱怨失踪反引号,而以前的版本(无a )没有。 How exactly is substitution working? 替代究竟是如何运作的? Is there a bug? 有bug吗?

Second, I guessed the range operator is cooler than I thought and can do any range of ASCII characters I choose, but {[.._} for example fails. 其次,我猜测范围运算符比我想象的要冷,可以选择任何范围的ASCII字符,但{[.._}例如失败。 Am I missing something to make this work or is this just a curiosity? 我错过了一些能让这项工作成功的事情,还是只是好奇心? Are there any more ranges besides letters/digits I can use? 除了我可以使用的字母/数字之外还有更多的范围吗? and if not, why not do nothing (fail, echo as is) for 'jumping' from caps to lower? 如果没有,为什么不做什么(失败,回声)从'上限'到下限?

The \\ is being generated; 所述\\ 正在生成; however, it subsequently appears to be treated as escaping the following space. 然而,它似乎被视为逃避以下空间。 Compare: 相比:

$ printf '%s\n' 'Z' '[' ']' '^' '_' '`' 'a'
Z
[
]
^
_
`
a
$ printf '%s\n' {Z..a}
Z
[

]
^
_
`
a

The extra blank line following the [ is the space escaped by the backslash generated by {Z..a} . [之后的空白行是{Z..a}生成的反斜杠转义的空格。

A special variable obase can be used with bc to print almost any character range(s): 一个特殊的变量obase可以与bc一起使用来打印几乎任何字符范围:

for n in {91..95}; do printf "\x$(echo "obase=16; $n" | bc)"; done

Result: 结果:

[\]^_

https://www.gnu.org/software/bc/manual/html_mono/bc.html#TOC6 ↳https //www.gnu.org/software/bc/manual/html_mono/bc.html#TOC6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM