Opening multiple tabs in gnome terminal with complex loop commands

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

command "complex argument" 

If I want to run gnome-terminal by passing this argument to it, it will look like this:

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

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

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

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

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

+4
source share
5 answers

I found a solution: arrays. They can do magic.

 # initial arguments command=(gnome-terminal -e 'command "complex argument"') ... # add extra arguments command=("${command[@]}" --tab -e 'command "complex argument2"') ... # execute command "${command[@]}" 
0
source

I just ran into this problem and stumbled upon this entry trying to fix it. If the "complex argument" depends on the shell extension, I believe that you will need to start the shell with the command passed to gnome-terminal. For instance:

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

You can run bash or any other shell instead of sh. See this exhange column for more examples.

+3
source

Not sure if this will help, since the post is about a year old, but I had a similar problem with the gnome-terminal script in BASH. My answer is similar to riachdesign, but escape characters are different. Here is what I did:

 gnome-terminal -e bash -c "/home/someprogramtorun /home/user/'$dir'/filetopasstoprogram.txt" 

If I did not add single quotes around $ dir (for example, $ dir vs. '$ dir'), the string would execute shorthand (i.e. would not pass the contents of the variable to the string).

Hope this helps.

+2
source

AFAIK, you may need to avoid double quotes (or single quotes, regardless of what you use around $ args), for example. ('command \ "complex argument1 \"' --tab -e 'command \ "complex argument2 \"')

0
source

Look at this ruby ​​stone that does just that: https://github.com/Achillefs/elscripto

0
source

All Articles