13

I am trying to write a command that will launch a few terminal tabs, execute something in each tab, and have each tab stay open after the command in finished, so I can look at the output and type more commands in each tab

something like this:

gnome-terminal --tab -e "ls -a" --tab -e "ls"

but the problem with this is that the tabs close as soon as the "ls" commands finish. Does anyone know how to make the tabs stay open?

3 Answers 3

11

Gnome-terminal can either execute a command or open a shell, but not both.

There is a workaround to do both by encapsulating the command and subsequent invocation of the shell into one command.

$ gnome-terminal -e "bash -c \"echo foo; echo bar; exec bash\""

For more alternatives read my answer to a similar question on stack overflow: https://stackoverflow.com/questions/3512055/avoid-gnome-terminal-close-after-script-execution/3531426#3531426

11

If you have xdotool and wmctrl installed, then the following shell script might work:

#!/usr/bin/env bash

window="$(xdotool search --class gnome-terminal | head -1)"
xdotool windowfocus $window
xdotool key ctrl+shift+t
xdotool type "$*"
xdotool key Return

I use it like this:

$ run-in-new-tab 'ls -l'

I found this idea on Trustin Lee's blog.

3
  • Thanks. This works pretty well. I have to add a sleep 1 after the ctrl+shift+t to get it to work for me though. I'll accept this if nothing else comes up soon
    – Mark
    Aug 4, 2010 at 5:07
  • Yeah - i found i had to have sleep 2 to make it work 100% of the time. Great solution though - i'm now opening 8 tabs, which ssh into various servers, start various processes etc, using this: my morning setup is now totally scripted :) Feb 21, 2013 at 10:00
  • Actually wmctrl is not required.
    – nedim
    Jul 14, 2016 at 10:11
1

For me lesmana's answer didn't work for multiple tabs (at least with 3.28.2). So, I found a workaround:

gnome-terminal --tab [-t title1] -- bash -c "gnome-terminal --tab [-t title2] -- bash -c \"ls; exec bash\"; ls -a; exec bash"

This opens a tab and executes again a gnome-terminal command that opens a second tab and executes the second tab's command, comes back to first, executes the command meant for first tab.

You must log in to answer this question.

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