Yun Sheng's Site
A Little Bit of This, A Little Bit of That

Fish Practice

tldr

  • string replace FROM TO input
  • string split SEP input

What is this about

I’ve been switching to fish for a while. The reason I switched was because I wanted to be able to comfortably write for loops in the interactive shell, utilize various string functions. Unfortunately the only thing I got used to is to do ctrl-f to autocomplete. This is a small drill I came up with to try to improve a little using fish

The adhoc drill

  1. Generate files from a.txt to z.txt
  2. Rename all from *.txt to *.yo.txt
  3. Replace all *.yo.txt to *.sup.txt

Step 1: Generate files from a.txt to z.txt

This step took me a bit longer than I expected. Turns out there is no simple way in fish to generate a to z. the fish native way would be string split "" "abcdefghijklmnopqrstuvwxyz" to tap into bash it would be bash -c "echo {a..z}"

Let’s go with bash

1
set xs (bash -c "echo {a..z}")

Now I want to for loop $xs and create the .txt files

1
2
3
for x in (string split ' ' $xs)
    touch "$x.txt"
end

Step 2: Rename all from *.txt to *.yo.txt

This step was nice, string split splits a.txt into a txt, and since fish is 1-based index, I set name to the filename without the extension.

Then it’s the mv command

1
2
3
4
for x in (ls)
      set name (string split '.' $x)[1]
      mv "$name.txt" "$name.yo.txt"
  end

Step 3: Rename all from *.yo.txt to *.sup.txt

I could do the same trick, pull out the name and manually write the sup part in mv, but instead I wanted to try out string replace

1
2
3
4
for x in (ls)
    set new (string replace 'yo' 'sup' $x)
    mv $x $new
end
Update: 2026-04-11
Tags: