Skip to content
MagickCmdErrors › Command works in bash but fails in PowerShell or…

Command works in bash but fails in PowerShell or cmd.exe

The ^ > < ! and % characters in ImageMagick geometry are shell metacharacters. How to quote them correctly in PowerShell, cmd.exe and inside .bat files.

You copied a command that works fine on someone's Mac and on Windows it does nothing, produces a stray file, or errors out.

The cause

ImageMagick's geometry suffixes collide with shell syntax:

CharacterImageMagick meaningShell meaning
>only shrink if largerredirect output to a file
<only enlarge if smallerread input from a file
^fill the box, overflowescape character in cmd.exe
!force exact sizehistory expansion in interactive bash
%percentage, and filename countersvariable expansion in cmd/bat

The shell processes these before ImageMagick ever sees them.

What actually happens

magick in.jpg -resize 800x600> out.jpg

Bash reads that as "run magick in.jpg -resize 800x600 and redirect its output into a file named out.jpg". You end up with an unconditionally resized image and a stray empty file. No error, wrong result — the worst kind of failure.

Correct quoting per shell

# bash / zsh / sh
magick in.jpg -resize '800x600>' out.jpg

# PowerShell
magick in.jpg -resize '800x600>' out.jpg

# cmd.exe
magick in.jpg -resize "800x600>" out.jpg

Single quotes are the safest choice in bash and PowerShell because they suppress all interpretation. cmd.exe does not understand single quotes at all — it needs double quotes.

The percent problem in .bat files

Quoting does not protect % in cmd.exe. Inside a .bat or .cmd file, every literal percent must be doubled:

REM at the prompt
magick in.jpg -crop 400x400 +repage tile_%02d.jpg

REM inside a .bat file
magick in.jpg -crop 400x400 +repage tile_%%02d.jpg

The same applies to loop variables — %f at the prompt becomes %%f in a file — and to identify -format strings.

Parentheses

ImageMagick's sub-expression parentheses are also shell-significant. Older tutorials escape them for bash:

magick in.jpg \( logo.png -resize 20% \) -composite out.jpg

That backslash escape does not work in PowerShell or cmd. Quoting them works everywhere:

magick in.jpg '(' logo.png -resize 20% ')' -composite out.jpg

This is why the generator on this site emits quoted parentheses — one form that is correct in all three shells.

PowerShell specifics

  • PowerShell splits arguments differently from cmd. If an argument contains spaces and metacharacters, single-quote the whole thing.
  • $ starts a variable in double-quoted PowerShell strings. Use single quotes for any ImageMagick format string containing $.
  • PowerShell 7 handles native-command arguments more predictably than 5.1. If a command works in 7 but not 5.1, quoting is usually the difference.
  • --% stops PowerShell parsing entirely: magick --% in.jpg -resize 800x600^ out.jpg. A blunt instrument, but useful when pasting a long command verbatim.

Batch loops

# bash
mkdir -p out
for f in *.jpg; do magick "$f" -resize '1600>' "out/$f"; done

# PowerShell
New-Item -ItemType Directory -Force out | Out-Null
Get-ChildItem *.jpg | ForEach-Object { magick $_.FullName -resize '1600>' "out/$($_.Name)" }

# cmd.exe (at the prompt — double the % in a .bat file)
if not exist out mkdir out
for %f in (*.jpg) do magick "%f" -resize "1600>" "out\%~nxf"

The generator on this page has a shell selector in the header — pick yours and every command is re-quoted automatically.


Related

Copied