Search code examples
zshparameter-expansionzsh-alias

Substitute a file extension in a zsh alias


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!


Solution

  • 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%.*}.)