Skip Navigation
General Programming Discussion @lemmy.ml

Returning (echoing) lists in Fish shell

I'm new to Fish and really liking it so far but the list type is really confusing me.

Something that I find really non-intuitive about fish is the list data type.

You can set a list like this

 sh
    
set my_list a b c
echo my_list[1] #a

  

But if you try this it doesnt work:

 txt
    
function mklist
  echo a b c
end
set lst (mklist)
echo $lst[1] # a b c

  

Putting the echo in quotes doesnt work either.

You can do:

 txt
    
function mklist
    echo a b c
end
set lst (string split " " -- (mklist))
echo $lst[1] # a

  

But needing to always split your return values is kinda terrible.

So it seems like what fish expects you to do is echo multiple lines.

 txt
    
function mklist
    echo a
    echo b 
    echo c
end
set lst (mklist)
echo $lst[1] # a

  

Its just very weird to me that it doesnt understand a comma delimited string to be a list.

I feel like I must be missing something.


Edit: FWIW I think I get why they designed it like this after thinking some more. It just feels weird.

This requires you to be explicit about returning lists. Otherwise any echo with spaces would be treated as a list and not a single value

Comments

2