Opening multiple terminal tabs and running command
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
and rails 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.
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.