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.
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.
Not sure this will help out as the post is about a year old, but I had a similar issue scripting gnome-terminal in BASH. My answer is similar to riachdesign, but the escape characters differ. Here's what I did:
gnome-terminal -e bash -c "/home/someprogramtorun /home/user/'$dir'/filetopasstoprogram.txt"
If I didn't add the single quotes around $dir (e.g., $dir vs. '$dir') the line would execute verbatim (i.e., it would not pass the variable's contents into the line).
Hopefully that helps.
AFAIK, you may need to escape the double quotes (or single quotes, whatever you use around $args), like so. ('command \"complex argument1\"' --tab -e 'command \"complex argument2\"')
Have a look at this ruby gem that does exactly that: https://github.com/Achillefs/elscripto
I found the 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[@]}"
来源:https://stackoverflow.com/questions/6611542/opening-multiple-tabs-in-gnome-terminal-with-complex-commands-from-a-cycle