Super User is a question and answer site for computer enthusiasts and power users. 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've made a small bash script to automatically split my two monitors with imagemagick when I take a screenshot.

#! /bin/bash
convert "$1" -crop 1024x600+0+0 +repage "${1//.png/}-left.png"
convert "$1" -crop 1280x1024+1024+0 +repage "${1//.png/}-right.png"

This works great, but will only rename it properly if it's a png (which in this case isn't an issue). Is there anyway to make it work so it'd move the file extension to the end? So if I called this script (let's call it splitimage) on a file oldscreenshot.jpg it'd give me oldscreenshot-left.jpg and oldscreenshot-right.jpg?

share|improve this question
up vote 1 down vote accepted

Yes, using the ${param##word} and ${param%word} expansions. The first will get rid of the extension, the last will get rid of everything but the extension

#! /bin/bash

convert "$1" -crop 1024x600+0+0 +repage "${1%.*}-left.${1##*.}"
convert "$1" -crop 1280x1024+1024+0 +repage "${1%.*}-right.${1##*.}"


$ var=foo.jpg; echo "No extention: ${var%.*} | Extention only: ${var##*.}"
No extention: foo | Extention only: jpg
share|improve this answer
image-1.5-final.png – Ignacio Vazquez-Abrams Jan 25 '12 at 5:14
right....thanks – SiegeX Jan 25 '12 at 7:05

Yes. But use stripping in your parameter expansion (not brace expansion, that's something completely different) instead of replacement.

convert "$1" -crop 1024x600+0+0 +repage "${1%.*}-left.${1##*.}"
share|improve this answer
Parameter expansion. I couldn't remember what it was called and thus couldn't search for it. – Rob Jan 25 '12 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .