Opening multiple terminal tabs and running command
Solution 1
I would recommend using tmux
with tmuxinator
, it will does the job for you, and you can rely on terminator
layouts too!
For terminator
layout checkout mhnagaoka's answer here askubuntu too:
After setting up your layout, right-click on any terminal background and choose Preferences → Layouts tab and click on Add button.
Give it a name and hit Close.
This should create the mentioned
~/.config/terminator/config
file.Now you can start terminator using the saved layout using:
terminator -l yourLayout
(replace yourLayout with whatever you chose on step 2).(optional) Edit the
~/.config/terminator/config
file so that where it says[layouts]
and nested below it[[yourLayout]]
, rename yourLayout to default and remove/rename the previous default layout. Now, when Terminator starts without any parameters, it will load your custom[[default]]
layout!
Solution 2
To summarize a couple of the other answers and their comments, here is what I ended up with:
#!/bin/bash
cd /path/of/my/stuff
tab=" --tab"
options=()
cmds[1]="echo Banana"
cmds[2]="echo Cat"
for i in 1 2; do
options+=($tab -e "bash -c '${cmds[i]} ; bash'" )
done
gnome-terminal "${options[@]}"
exit 0
Note that I took away the 'master' tab, and the profiles, and the titles (which appear to be deprecated in gnome-terminal
).
Solution 3
Try this:
options+=($tab --title="${titles[i]}" -e "bash -c \"${cmds[i]} ; bash\"" )
otherwise the whole expression after -e will be interpreted as the command.
To include aliases from .bashrc use -ic
instead of c
Solution 4
The argument you're giving the -e option is "bash -c command; bash"
including the quotes. It interprets that whole string as the name of a command! Try this instead: -e "bash -c 'command ; bash'"
. This way what gets run on your terminal window is command
, and after that runs, you're given a daughter shell, which I assume is what you want. Incidentally, you can also say 'command & bash'
; this will run the command on the background and give you the daughter shell right away.
Related videos on Youtube
Brary
Updated on September 18, 2022Comments
-
Brary over 1 year
I need to open multiple terminal tabs, give them titles, go to a directory, and make each tab run a command.
I am new to Linux and shell scripting, after searching online and checking some solutions, I made this script (EDITED based on answers below):
#!/bin/bash cd /media/Extra/Project tab=" --tab-with-profile=Default" options=(--tab --title=Terminal) cmds[1]="'rails s'" titles[1]="Server" cmds[2]="'rails c'" titles[2]="Console" for i in 1 2; do options+=($tab --title="${titles[i]}" -e "bash -c \"${cmds[i]} ; bash\"" ) done gnome-terminal "${options[@]}" exit 0
It opens the tabs, names them, but fail to execute the commands generating this error:
There was an error creating the child process for this terminal
Another shortcoming is that if I halted the running command it closes the tab, which I don't want. I need to be able to stop the command and run it again within the same tab.
What is wrong with the script? Is there another simpler way to do that?
Note: If I removed the (-e "\"bash -c ${cmds[i]} ;bash\"") part from the command, it opens the tabs in the given directory and name them, with no errors.
-Edit-1:
After applying @Tuknutx answer below and editing the script, the error doesn't appear anymore, but it gives me
bash: rails c: command not found
andrails s
creates a new rails app instead of starting the rails server, I am using .rmvrc to select a gemset once this folder is accessed.-
Admin almost 10 yearsIf you want to make your script run on your ubuntu boot: - you can run you script as an upstart service(help.ubuntu.com/community/UbuntuBootupHowto) or: using "startup applications" in your desktop environment
-
Admin about 8 yearsif i write
cmds[1]="'ping 192.168.9.9'"
it gives me the same error but i resolved it by removing single quotation. So if i writecmds[1]="ping 192.168.9.9"
It works. -
Admin about 8 years@Brary but i don't understand why it opens three tabs-one default and another two that set in script. Because we define
tab=" --tab-with-profile=Default"
??
-
-
Brary almost 10 yearsI changed this
options+=($tab --title="${titles[i]}" -e "\"bash -c ${cmds[i]} ;bash\"" )
intooptions+=($tab --title="${titles[i]}" -e "\"bash -c '${cmds[i]} ;bash'\"" )
and it gave me the same error .. am I doing it right? -
Brary almost 10 yearsThe error doesn't appear anymore, thanks. But it gives me
bash: rails c: command not found
and 'rails s' creates a new rails app instead of starting the rails server. This changes the scope of the question, I will edit it with ur modification for the script -
TuKsn almost 10 yearsDid it work if you try
rails c
on a terminal without anything else? Perhaps it should berails -c
? -
Silvio Levy almost 10 yearsNot quite correct: remove \" before the first bash and \" after the ' after the second bash.
-
Brary almost 10 yearsTried that didn't work. I even tried
rvm list gemsets
and it saysNo command 'rvm' found
.. run the same commandrvm list gemsets
in the first tab that opens without running a command and it worked! does it run the command before the environment loads and the .rvmrc file is loaded? -
Brary almost 10 yearsThanks, I edited the question and changed the scope of it to include rails and rvm.
-
nyxee over 4 yearsThe output of
$tab -e...
Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
` Use “-- ” to terminate the options and put the command line to execute after it.. that's an output but the comands get executed. When i use
$tab -- ` the command doesn't get executed and the terminal dsisplays:There was an error creating the child process for this terminal. Failed to execute child process “bash -c 'echo Banana ; bash'” (No such file or directory)