Substitute a file extension in a zsh alias

420 views Asked by At

I'm trying to create an alias for cwebp to run from zsh that converts an input image file, to an output image file of the same name, but with the .webp file extension:

# in .zshrc
alias cwebphoto='cwebp -preset "photo" -short -noalpha $1 -o ${1%.*}.webp'

Then in zsh

> cwebphoto hello.png

Returns a converted file named .webp How can I instead return a file named hello.webp?

Any help is appreciated!

1

There are 1 answers

2
chepner On BEST ANSWER

You want a function instead.

cwebphoto () {
  cwebp -preset "photo" -short -noalpha $1 -o ${1%.*}.webp
}

(In zsh, you can also use $1:r in place of ${1%.*}.)