Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fish shell: how to append an element to an array

Tags:

fish

I'm trying to append an element to an array.

What I tried is:

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

but that does not add a new element. It creates a single variable containing all of children then a space and $line[$i].

like image 794
Adham Zahran Avatar asked Apr 25 '18 17:04

Adham Zahran


1 Answers

Using fish version 2.7.1-1113-ge598cb23 (3.0 pre-alpha) you can use set -a (append) or 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

Output:

joe elias matt
joe elias matt tiny tim bob

You could also use the string command which should work on most recent versions of fish.

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

Output:

joe elias matt
joe elias matt tiny tim bob
like image 139
David Nielsen Avatar answered Oct 01 '22 01:10

David Nielsen