Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a very simple script which does this:

mogrify -resize $1x$2^ -gravity center -crop $1x$2+0+0 $3

so that I can call it in this way:

cropresize.sh 110 110 *.png

the problem is that the shell expands the *.png pattern instead of passing it as it is to the script.

How can I achieve this (script, alias or any other equivalent solution is fine)?

share|improve this question
up vote 7 down vote accepted

Since the shell performs glob expansion before the arguments are handed over to the command, there's no way I can think of to do it transparently: it's either controlled by the user (quote the parameter) or brute-force (disable globbing completely for your shell with set -o noglob).

You're looking at the problem from the wrong end. Change your script to accept multiple filename arguments:

shift 2
mogrify -resize ${x}x${y}^ -gravity center -crop ${x}x${y}+0+0 "$@"
share|improve this answer
After some thought I came to this conclusion but I couldn't find how to specify "the rest of the arguments after the first two", I was just missing shift :) – Matteo Riva Mar 3 '11 at 14:59
For the record you can disable globbing for the specific shell script being run by simply adding set -o noglob at the beginning of the script. That will make it possible to pass arguments without expansion. – Mahn Apr 22 at 17:37

Have you tried

cropresize.sh 110 110 \*.png

But I can only see zsh (not bash) expanding it like you say.

share|improve this answer
Well I can even enclose the argument in single quotes like '*.png' but I'd really like it to be completely transparent as if it were a builtin command – Matteo Riva Mar 3 '11 at 14:31
Are you using bash or some other shell? Because in bash what you ask works, but in zsh (which I use) even echo *.png tries expanding to all png files. – ryuslash Mar 3 '11 at 14:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.