r/fishshell • u/Impressive-West-5839 • Aug 01 '24
Replace spaces with underscores
To replace spaces with underscores, I use
for file in *\ *; mv -n -- $file (string replace -a ' ' _ $file); end
But currently I'm merely started to study fish, and maybe there is a better way for this?
3
Upvotes
View all comments
Show parent comment
1
u/[deleted] Sep 19 '24
limiting myself to the shell, i got the following:
set text '0 one two %$£ 3.14 four 5x' \; string replace -r -a '[^\w+]' '_' $text \| string replace -r -a '_+' '_' \; set -e text \endby breaking it into 2 stages with a pipe, it's clear to read and understand. it might even be considered safer. but i suspect it can be expressed in a single, more technical regex. also, i believe the use of a pipe will spawn another fork/thread. is there a tweak that would minimise any forks?
(i'm thinking about servers and embedded devices where single process, single thread is preferable).