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
fish_add_path is a simple way to add more components to fish's PATH . It does this by adding the components either to $fish_user_paths or directly to $PATH (if the --path switch is given). It is (by default) safe to use fish_add_path in config.
Switching to fish? If you wish to use fish (or any other shell) as your default shell, you need to enter your new shell's executable /usr/local/bin/fish in two places: add /usr/local/bin/fish to /etc/shells. change your default shell with chsh -s to /usr/local/bin/fish.
To be able to run fish scripts from your terminal, you have to do two things. Add the following shebang line to the top of your script file: #!/usr/bin/env fish . Mark the file as executable using the following command: chmod +x <YOUR_FISH_SCRIPT_FILENAME> .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With