How to rename file extensions in fish in a for loop?

Here is the equivalent bashscript I am trying to convert to fish:

for j in *.md; do mv -v -- "$j" "${j%.md}.txt"; done

Here is what I tried:

for file in *.md
    mv -v -- "$file" "{{$file}%.md}.txt"
end

But it just finishes renaming all the files:

'amazon.md →' {{amazon.md}%. md} .txt

How to do it right?

+4
source share
3 answers

I found an alternative solution:

for file in *.md
    mv -v -- "$file" (basename $file .md).txt 
end

It works like a charm!

+4
source

The fish shell does not support parameter expansion operations such as bash. The fish shell philosophy allows existing teams to do work, rather than reinvent the wheel . You can use sed, for example:

for file in *.md
    mv "$file" (echo "$file" | sed '$s/\.md$/.txt/')
end
+1
source

To do this simply with fish:

for j in *.md
    mv -v -- $j (string replace -r '\.md$' .txt $j)
end
+1
source

Source: https://habr.com/ru/post/1649160/


All Articles