Search code examples
macoszshoh-my-zsh

how to pass customized zsh function argument to in-block zmv commands?


I am creating a zsh script that batch resize, convert and rename set of images for web dev.

The zsh function is structured like below:

function img() {
    # lines of "magick -resize" and "magick mogrify -format"

    autoload zmv
    zmv '(*).(*)' '$1.$2'
}

How can i pass an argument from the command like img "something" so the zmv in-function command can be:

zmv '(*).(*)' 'something$1.$2'

Thanks!


Solution

  • This will prefix the second argument to zmv with the first argument passed to img:

    img() {
      zmv '(*).(*)' $1'$1.$2'
    }
    

    This works because the first $1 is unquoted and thus evaluated before it is passed to zmv, whereas '$1.$2' is in single quotes and thus passed as a literal string.