My login script ~/.profile ends with the command

exec startx > ~/.xsession-errors 2>&1

I would like to run a second command foo when startx is done so I tried

exec { startx > ~/.xsession-errors 2>&1; foo; }

but that results in a syntax error. Any clues?

  • 2
    How about exec sh -c 'startx > ~/.xsession-errors 2>&1; foo' Jul 8, 2015 at 20:43
  • What about putting it in your shell's .logout file? Or in your ~/.xsession file?
    – Jeff Schaller
    Jul 8, 2015 at 20:51
  • @JeffSchaller As far as I understand, ~/.bash_logout is only run when exit or Ctrl-D is invoked. Jul 8, 2015 at 21:15
  • @glennjackman That will indeed work, thanks! However, I think yaegashi's solution is bit easier to read. Jul 8, 2015 at 21:21

1 Answer 1


It won't return to the script when exec'ed command terminates. Therefore you can use exec only for the last command run in the script.

startx > ~/.xsession-errors 2>&1
exec foo
# You get here if exec foo failed
echo "Unable to exec foo" >&2

You must log in to answer this question.

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