Opening multiple tabs in gnome terminal with complex commands from a cycle

petersohn picture petersohn · Jul 7, 2011 · Viewed 7.9k times · Source

I have a command that needs to be called like this:

command "complex argument"

If I want to run gnome-terminal passing it this argument, it goes like this:

gnome-terminal -e 'command "complex argument"'

I want to open multiple tabs in the terminal, executing this command with different arguments each time. This works this way:

gnome-terminal -e 'command "complex argument1"' --tab -e 'command "complex argument2"'

But the problem comes if I want to execute it with a script, where I get the parameters for each tabs from a cycle (i.e. the number of tabs is variable). My basic idea was that I collect the arguments to a single variable, then pass it to gnome-terminal. But I don't know how to do this leaving all the nested quoted arguments intact. Either everything is compressed in one argument (if I call gnome-terminal "$args"), or it falls apart by every whitespace (if I call gnome-terminal $args).

Is there any way to compose such complex arguments in bash? Or, alternatively, is there any way to send IPC messages to gnome-terminal, telling it to open a new tab and execute a command? I know I can do this with Konsole, but now I want to do it with gnome-terminal.

Answer

Matt Liberty picture Matt Liberty · Feb 1, 2012

I just ran into this same problem and came across this post while trying to fix it. If "complex argument" relies upon shell expansion, I believe that you will need to start a shell with the command passed to gnome-terminal. For example:

gnome-terminal \
--tab -e "sh -c 'command \"complex argument1\"'" \
--tab -e "sh -c 'command \"complex argument2\"'"

You can start bash or any other shell instead of sh. For more examples see this stack exhange post.