linux提取一行数组第一个字符,数组 – 获取数组的第一个索引

My attempts seem to overcomplicate such an easy task

这个任务可能看起来很简单,但可能不是一个常见的用例,因此没有语法上的容易,特别是如果你可以使用其他内置功能来实现它.

你有很多选择,还有一个:

put_first_index_in () {

printf -v "${1?}" "%s" "${2?}"

}

put_first_index_in first_index "${!arr[@]}"

What is actually happening in ${!arr[@]:0:1}?

If the first character of parameter is an exclamation point (!),

and parameter is not a nameref,it introduces a level of variable

indirection. Bash uses the value of the variable formed from the rest

of parameter as the name of the variable; this variable is then

expanded and that value is used in the rest of the substitution,

rather than the value of parameter itself. This is known as

indirect expansion.

随着!作为第一个字符,支持三种可能的东西:${!parameter}(上面给出的间接扩展),${!prefix *},${!prefix @}(扩展为与前缀匹配的变量名)和${ !name [*]},${!name [@]}(展开到名称数组的索引).

文档建议只有${!parameter}支持进一步替换,因为它只是为此提及而不是为其他提及.所以bash尝试执行以下操作:

>展开arr [@]

>对从(1)获得的字符串应用间接扩展

>从间接扩展获得的字符串中获取0:1子字符串

由于标识符中不是有效字符,因此我们会收到该错误:

$foo=SHELL

$echo ${!foo}

/bin/zsh

$echo ${!foo:0:1}

/

$foo="SHELL "

$echo ${!foo}

bash: SHELL : bad substitution

$arr=([2]=SHELL bbb ccc ddd)

$echo ${!arr[@]:0:1}

bash: SHELL bbb ccc ddd: bad substitution

因此,这只适用于单个元素的数组,例如:

$arr=([2]=SHELL)

正如所料:

$echo ${!arr[@]:0:1}

/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值