Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm attempting to write a script for use with HandbrakeCli to convert .mkv-files to XBOX 360 compatible mp4-files.

for f in "$@"
    echo "$f"
    HandbrakeCli -i "$f" -o ~/Movies/Handbrake/"${f%.*}.mp4" --preset "Normal" --mixdown "stereo" --subtitle "1" --width 720 --keep-display-aspect

The problem is that the destination filename (according to output from HandbrakeCli) includes the full path to the input file. Naturally I'm only interested in the filename without extension since I add .mp4.

I'm not used to shell scripting so help is very much appreciated!

share|improve this question
Also see this discussion of this question on stackoverflow:… – guenter Oct 3 '11 at 11:11

2 Answers 2

up vote 3 down vote accepted

First remove the path:


Then change the extension:


(Not necessarily in that order, of course.)

share|improve this answer

Since you know the extension of your input file, you can say

new=~/Movies/Handbrake/"$(basename "$f" .mkv)".mp4
share|improve this answer

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.