Reputation: 59587
How would you iterate over a string in the fish shell?
Unfortunately iterating over a string isn't as straight forward as iterating over a list:
↪ for c in a b c
echo $c
end
a
b
c
↪ for c in abc
echo $c
end
abc
Upvotes: 7
Views: 2043
Reputation: 59587
The for
loop in fish operates on a list.
for VARNAME in [VALUES...]; COMMANDS...; end
The builtin string
command (since v2.3.0) can be used to split a string into a list of characters.
↪ string split '' abc
a
b
c
The output is a list, so array operations will work.
↪ for c in (string split '' abc)
echo $c
end
a
b
c
A more complex example iterating over the string with an index.
↪ set --local chars (string split '' abc)
for i in (seq (count $chars))
echo $i: $chars[$i]
end
1: a
2: b
3: c
Upvotes: 10