数组 – 获取数组的第一个索引

我有一个未从0索引的数组:

arr=([2]=aaa bbb ccc ddd)

我需要得到数组的第一个索引.有很多事情我尝试过并且有效:

for index in "${!arr[@]}"; do
    first_index=$index
    break
done
first_index=$(awk '{ print $1 }' <<< "${!arr[@]}")
first_index=$(cut -d' ' -f1 <<< "${!arr[@]}")
first_index=${!arr[@]}
first_index=${first_index%% *}
ind=("${!arr[@]}")
first_index=${ind[@]:0:1}

我真正想要的是:

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

鉴于这个语法对于数组${arr [@]:0:1}非常有效并且非常干净:

>在没有外部工具,临时数组,临时变量,循环等的情况下,是否有类似的,更清晰的方法为索引执行此操作?我的尝试似乎使这么简单的任务过于复杂
> ${!arr [@]:0:1}实际发生了什么?

最佳答案

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}?

Indirect variable expansion

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}
/

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...