The Computer Oracle

Programmatically open tab in gnome-terminal, execute command, and have tab stay open

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Ominous Technology Looping

--

Chapters
00:00 Programmatically Open Tab In Gnome-Terminal, Execute Command, And Have Tab Stay Open
00:32 Answer 1 Score 11
00:50 Accepted Answer Score 11
01:13 Answer 3 Score 1
01:37 Thank you

--

Full question
https://superuser.com/questions/178617/p...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #shellscript #gnometerminal

#avk47



ANSWER 1

Score 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.




ACCEPTED ANSWER

Score 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




ANSWER 3

Score 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.