首页 > 解决方案 > 鱼壳:如何将元素附加到数组

问题描述

我正在尝试将元素附加到数组中。

我尝试的是:

 for i in (seq 10)
            set children $children $line[$i]
 end

但这并没有添加新元素。它创建一个包含所有孩子的单个变量,然后是一个空格和 $line[$i]。

标签: fish

解决方案


使用 fish 版本 2.7.1-1113-ge598cb23 (3.0 pre-alpha),您可以使用set -a(append) 或set -p(prepend)。

set -l array "tiny tim" bob
set -l children joe elias matt

echo $children
for i in (seq 2)
    set -a children $array[$i]
end
echo $children

输出:

joe elias matt
joe elias matt tiny tim bob

您还可以使用适用于最新版本 fish 的 string 命令。

set -l array "tiny tim" bob
set -l children joe elias matt

echo $children
for i in (seq 2)
    set children (string join " " $children $array[$i])
end
echo $children

输出:

joe elias matt
joe elias matt tiny tim bob

推荐阅读