1

in simple I want to execute one command over many files and am trying to do that with a for do done loop.

dwebp foo.webp -o ./PNGs/foo.png is the command that I want to execute over all files that correspond to *.webp in my current directory.

I am able to get the /PNGs/foo.png working by doing

for f in *.webp; do echo "$f" "${f%.webp}.png"; done. I'm now however not able to use $f as the original file name (which is still needed).

The fact that I am using the ${f%.webp}.png without really understanding why or how it works doesn't help... So, does anyone know how I'd be able to run the dwebp foo.webp -o ./PNGs/foo.png command with a for do done loop, or a better way to achieve the same thing?

1
  • 2
    "${f%.webp}.png" doesn't change f's value. You can still use $f as the original filename. Commented Jan 12, 2021 at 15:48

1 Answer 1

1

For an explanation on how ${f%.webp} works, see Parameter Expansion.

${f%.webp} does not modify the value of ${f}, consider:

$ f=foo.webp
$ echo "${f%.webp}.png"
foo.png
$ echo "${f}"
foo.webp

Net result is that you could try something like:

for f in *.webp
do
    echo "${f}"
    dwebp "${f}" -o "./PNGs/${f%.webp}.png"
done

Or if you'll need to perform the same parameter expansion a few times, you can do it once and store in a new variable, eg:

for f in *.webp
do
    newf="${f%.webp}.png"
    echo "original file : ${f} / new file : ${newf}"

    dwebp "${f}" -o "./PNGs/${newf}"
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.