Ubuntu – Opening multiple terminal tabs and running command

bashgnome-terminalrailsrvmscripts

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.

Best Answer

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:

  1. After setting up your layout, right-click on any terminal background and choose PreferencesLayouts tab and click on Add button.

  2. Give it a name and hit Close.

  3. This should create the mentioned ~/.config/terminator/config file.

  4. Now you can start terminator using the saved layout using: terminator -l yourLayout (replace yourLayout with whatever you chose on step 2).

  5. (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!