Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

So I was writing this little nautilus script for transcoding video into mp3:

#! /bin/bash -x

if [ -z "$1" ]
    zenity --warning --text="Error - No file selected !"
    exit 1


exec ffmpeg -i "$1" -ab 256k "$BASEFILENAME.mp3" &&

if [ "$?" -eq 0 ]
    zenity --info --text="Converting successful"

The problem is, though the ffmpeg command is executed successfully the if [ "$?" -eq 0 ]

seems not to get triggered. Why is that? Is the && wrong or is it something else?

share|improve this question
up vote 11 down vote accepted

The only way that statement can be reached is if the exec itself fails; if it succeeds, the ffmpeg command replaces the shell completely. (Pedantically, the && will fail in that case also so it can't be reached at all.) You don't want to exec it, just run it.

share|improve this answer
I see, and without the && after the ffmpeg command it actually works. I still have one more question, if it's okay at this place - is there a way to display the stdout and stderr of ffmpeg somewhere without using a terminal window? Like in zenity f.e. – tesseract Jun 21 '11 at 20:41
You'd want to capture it with the $() construct into a variable, then pass that variable to zenity. Watch out for quoting, and zenity in my experience uses PanGo so you need to replace <, &, > with &lt;, &amp;, &gt; respectively. – geekosaur Jun 21 '11 at 20:45
Okay, thank you very much for your help :) – tesseract Jun 21 '11 at 20:48

The exec command statement replaces the current shell with command. That is, your script actaully terminates at line exec ffmpeg ...; remaining lines would be executed if and only if the ffmpeg command is not found on your PATH (or it cannot be launched for other reasons).

You can get more details on the exec bash built-in by typing help exec at the bash command prompt:

$ help exec
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
    Replace the shell with the given command.

    Execute COMMAND, replacing this shell with the specified program.
    ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,
    any redirections take effect in the current shell.
share|improve this answer
I get it, thanks for clearing that up – tesseract Jun 21 '11 at 20:42

According to this exec replaces the shell with the command you specify. So your script is never getting to the commands past the exec.

You don't need exec. Just specify the command.

share|improve this answer

You may keep the exec command if you put it into a subshell:

- exec ffmpeg -i "$1" -ab 256k "$BASEFILENAME.mp3" &&
+ (exec ffmpeg -i "$1" -ab 256k "$BASEFILENAME.mp3") &&
share|improve this answer
I see, but isn't the && still wrong there? – tesseract Jun 22 '11 at 8:26
Yes. And what's the point of keeping the exec? – jmtd Jun 22 '11 at 8:34

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.