How can I launch multiple xterm windows and run a command on each, leaving each window open afterward? How can I launch multiple xterm windows and run a command on each, leaving each window open afterward? unix unix

How can I launch multiple xterm windows and run a command on each, leaving each window open afterward?


I'd love to see a more elegant answer, but what I came up with does work:

xterm -e bash -c 'echo foo; exec bash'

Replace echo foo with the command of your choice, and you're good to go.


This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:

#!/bin/bash --init-filecommands to run

... and execute it as:

xterm -e /path/to/script# orgnome-terminal -e /path/to/script# orthe-terminal -e bash --init-file /path/to/script/with/no/shebang

My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.

Another option is cmdtool from the OpenWin project:

/usr/openwin/bin/cmdtool -I 'commands; here'# or/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash

... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.


Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.

Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window

See: https://superuser.com/a/610048