DEV Community

nabbisen
nabbisen

Posted on

Fish Shell: string replace

fish shell has powerful string modules.
The subcommands are lower / upper, trim, sub (string), split, join, repeat, length, match and so on.

Thus we can replace strings via fish shell.
It's easy :)

The usage is:

$ string replace [(-a | --all)] [(-f | --filter)] [(-i | --ignore-case)] [(-r | --regex)] [(-q | --quiet)] PATTERN REPLACEMENT [STRING...]
Enter fullscreen mode Exit fullscreen mode

Besides, my current fish version is 3.0.2.

Well, for example, this is how to replace a file name with one that the size information is added to:

$ string replace -r "\.jpg\$" "\-360x.jpg" "some-file-name.jpg"
Enter fullscreen mode Exit fullscreen mode

With iteration, it becomes more powerful 😉

$ for x in ./*.jpg; mv "$x" (string replace -ar "\.jpg\$" "\-360x.jpg" "$x"); end
Enter fullscreen mode Exit fullscreen mode

This one-liner program renames files with a regular expression pattern via string replace.

Top comments (0)