I'm making a script that combines taking input from the command line with the launch of other programs. So the script will go like this:

launch program
Read input from the terminal

The problem is that when I launch the program (even if I launch it in the background), the terminal looses focus and the launched program becomes active.

How do I launch a program keeping the focus on the terminal with a default Ubuntu command?

Thank you

  • Also you could try looking at this question.
    – iBelieve
    Commented Jan 8, 2013 at 0:11
  • Interesting question, I don't think there is any way to guarantee that you won't lose focus.
    – elias
    Commented Jan 8, 2013 at 18:59

1 Answer 1


If you know the title of the terminal, you can switch back to it after opening the program using the wmctrl command (it doesn't even look like the new program was in the foreground at all if the window opens instantly and you don't need the sleep command):

wmctrl -a <WIN-TITLE>

<WIN-TITLE> doesn't have to be the full title, for example, if the title was TESTING, TEST would work.

Depending on how long it takes to open the program, you might need to use the sleep command to delay the switch back to the terminal. You can use it like this: sleep <SECONDS>s.

To set the title of the terminal from within a script, use:

NAME="NEW_TITLE"; echo -en "\033]0;$NAME\a"

See this answer for more information.

For example, here is a little script to open a program in the background:

NAME="Opening program..."; echo -en "\033]0;$NAME\a"
sleep 0.75s
wmctrl -a "Opening program"
  • I already saw that option but I was searching for a default ubuntu command, so there was no need for installing a new program
    – Vndtta
    Commented Jan 8, 2013 at 0:47
  • Sorry, it was already installed on my system so I didn't realize it wasn't installed by default.
    – iBelieve
    Commented Jan 8, 2013 at 1:53
  • It's OK, if no one gives a better answer in a week I'll mark this one as good. Thanks for the answer by the way
    – Vndtta
    Commented Jan 8, 2013 at 8:23
  • 1
    Three years have passed. Maybe it's time to mark the answer as good :)
    – MSX
    Commented Apr 8, 2016 at 8:04
  • 8 years and counting ... :D
    – Paloha
    Commented Nov 24, 2021 at 16:02

You must log in to answer this question.

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