简体   繁体   English

Bash 参数扩展和数组索引操作

[英]Bash parameter expansion and array index operations

The ${parameter,,pattern} parameter expansion converts alphabetic characters in parameter to lower-case. ${parameter,,pattern}参数扩展将参数中的字母字符转换为小写。

On cygwin 1.7.11-1 Bash 4.1.10(4) and also on my debian squeeze Bash 4.1.5(1) ;cygwin 1.7.11-1 Bash 4.1.10(4)和我的debian squeeze Bash 4.1.5(1)上;
if i run the following, i get a curious result:如果我运行以下命令,我会得到一个奇怪的结果:

$ declare -a a=(Zero One Two Three); n=0; echo "${a[n],,}->${n}"; echo "${a[++n]}->${n}"; echo "${a[++n],,}->${n}"
zero->0
One->1
three->3
$

NB: similar results happen:注意:类似的结果发生:
for ,,* or ^^ case conversion;用于,,*^^大小写转换;
for some other expansions such as ${parameter##word} ;对于其他一些扩展,例如${parameter##word}
for using either prefix/postfix ++ or -- operator;用于使用前缀/后缀++--运算符;
for using $((++n)) instead of just ++n .使用$((++n))而不仅仅是++n

However, the length expansion ${#parameter} works as i might expect:但是,长度扩展${#parameter}的工作方式与我预期的一样:
in the above snippet, echo "${#a[++n]}->${n}" instead of echo "${a[++n],,}->${n} would yield 3->2 instead of three->3 ~& the length of a[2]="two" is indeed 3 characters.在上面的片段中, echo "${#a[++n]}->${n}"而不是echo "${a[++n],,}->${n}会产生3->2而不是three->3 ~& a[2]="two" 的长度确实是 3 个字符。


I imagine that the parameter expansion is happening twice.我想参数扩展发生了两次。 But why is this happening?但是为什么会这样呢?

I grabbed the latest bash source & it appears that this issue is resolved for this version:我抓取了最新的 bash 源代码,看来此版本已解决此问题:

$ ./bash --version
GNU bash, version 4.2.24(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
$

Note however that on my Debian Squeeze stable , I have:但是请注意,在我的 Debian Squeeze stable上,我有:

$ apt-cache policy bash
bash:
  Installed: 4.1-3
  Candidate: 4.1-3
  Version table:
 *** 4.1-3 0
        500 http://ftp.uk.debian.org/debian/ squeeze/main amd64 Packages
        100 /var/lib/dpkg/status
$
$ bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>


I think this means that currently on Debian Squeeze , the latest stable version of bash is 4.1.5(1) & contains this bug.我认为这意味着目前在Debian Squeeze上,bash 的最新稳定版本是4.1.5(1)并且包含此错误。 Also on Cygwin 1.7.11-1 currently the latest available bash package appears to be version 4.1.10(4) & contains this bug.同样在Cygwin 1.7.11-1上,当前最新可用的 bash package 似乎是版本4.1.10(4)并且包含此错误。 Of course the issue is easy to work around, so no real need to build the 4.2 sources.当然这个问题很容易解决,所以不需要构建4.2源代码。

I got我有

Zero->0
One->1
Two->2

However I think "${a[++n]}->${n}" may not be well defined.但是我认为 "${a[++n]}->${n}" 可能没有明确定义。

try substitute with尝试用

let n=$n+1 ; echo "${a[n],,}->${n}";

To inject the notion of sequence.注入序列的概念。

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

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