Open Terminal with multiple tabs and execute application
Use this variant of the script to do what you want:
#!/bin/bash
tab="--tab-with-profile=Default"
cmd="bash -c 'java RunRTSPClient';bash"
foo=""
for i in 1 2 3 4 5; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[@]}"
exit 0
Generally, a script like this:
#!/bin/bash
tab="--tab"
cmd="bash -c '<command-line_or_script>';bash"
foo=""
for i in 1 2 ... n; do
foo+=($tab -e "$cmd")
done
gnome-terminal "${foo[@]}"
exit 0
will open a new terminal with n tabs executing the <command-line_or_script>
in each tab. This can be very useful when you want for example to open a terminal with some tabs with the interpreter at a specific path (using cd /path
in the above script).
Also, read man bash
, this post and this post to understand the changes.
I have tested these scripts and they work.
I came up with my own answer. I think this is a better approach because:
- I can understand it. I'm not a bash expert and no explanation was given in the more popular answer for what is
...
,${}
,-e
, or@
- It allows you to easily customize the title and command for each tab
- Did I say it's a lot easier to understand?
Note that the ; $SHELL
at the end of each gnome-terminal command is what keeps the terminal window open. Otherwise it would immediately close.
Old version [I recommend the new version below, instead]:
- works on older versions of
gnome-terminal
, such as from Ubuntu 14.04. -
gnome-terminal
disabled the--title
option sometime on or after version 3.16.2 (see comment by Ivan Kozik under this answer, and see my comments below my answer), however, so although the rest of the script below does still work in modern versions ofgnome-terminal
and Ubuntu, setting the title of each tab with--title
does NOT. See the New version below for an alternative that works everywhere.
Old code (setting the tab title like this doesn't work anymore in Ubuntu 16 or 18 or later, and the --command
option is unfortunately also now deprecated):
title1="tab 1"
title2="tab 2"
title3="tab 3"
cmd1="cd /etc"
cmd2="cd ~/Documents"
cmd3="cd /usr/local"
gnome-terminal --tab --title="$title1" --command="bash -c '$cmd1; $SHELL'" \
--tab --title="$title2" --command="bash -c '$cmd2; $SHELL'" \
--tab --title="$title3" --command="bash -c '$cmd3; $SHELL'"
New version [USE THIS ONE!] (Added 7-11 Feb. 2020):
New code--works perfectly in all versions of the gnome-terminal
and terminator
terminals (and could be extended to work with more terminals if desired), and has been tested in Ubuntu 14.04, 16.04, 18.04, and 20.04. You can find the latest and best version of this in my personal "~/.bash_aliases" and "~/.bash_aliases_private files in my dotfiles project here. Read the readme, and search the "~/.bash_aliases" file for the section called "CUSTOM TERMINAL TABS & TITLES". In the "~/.bash_aliases_private" file, search for the section called "CONFIGURATION FOR gs_open_default_tabs
Bash function". See also the "open_programming_tools.sh" script and "open_programming_tools.desktop" desktop launcher file, both of which are used to quickly open up your terminal with all tabs as you've configured.
All of the below information past this point should work but is not up-to-date with my latest changes found in my repo just above. Take a look there for my latest techniques and scripts.
-
Setting the tab title like this below DOES work in ALL versions of gnome-terminal now, so it works fine in Ubuntu 16 and 18 and 20 and later!
-
See here and the references at the very bottom of this answer for a lot more details and further understanding: https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
-
Note that the
gnome-terminal
--command
(-e
) and--title
options are unfortunately now deprecated, hence this difficult work-around. Here's the warning I get if I callgnome-terminal
with one of the deprecated options from the command-line:# Option “--command” 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.
-
I'll be using an interesting custom
set-title
function with some special use ofexport
ed variables and the ~/.bashrc file since it gets automatically sourced each time an interactive (-i
) bash shell is opened.
1st, add this to the bottom of your ~/.bashrc
file:
Update the DEFAULT_TABS_TITLE
and DEFAULT_TABS_CMD
variables as you see fit.
# Function to allow a user to arbitrarily set the terminal title to anything
# Example: `set-title this is title 1`
set-title() {
# Set the PS1 title escape sequence; see "Customizing the terminal window title" here:
# https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
TITLE="\[\e]2;$@\a\]"
PS1=${PS1_BAK}${TITLE}
}
# Back up original PS1 Prompt 1 string when ~/.bashrc is first sourced upon bash opening
if [[ -z "$PS1_BAK" ]]; then # If length of this str is zero (see `man test`)
PS1_BAK=$PS1
fi
# Set the title to a user-specified value if and only if TITLE_DEFAULT has been previously set and
# exported by the user. This can be accomplished as follows:
# export TITLE_DEFAULT="my title"
# . ~/.bashrc
# Note that sourcing the ~/.bashrc file is done automatically by bash each time you open a new bash
# terminal, so long as it is an interactive (use `bash -i` if calling bash directly) type terminal
if [[ -n "$TITLE_DEFAULT" ]]; then # If length of this is NONzero (see `man test`)
set-title "$TITLE_DEFAULT"
fi
DEFAULT_TABS_TITLE1="tab 1"
DEFAULT_TABS_TITLE2="tab 2"
DEFAULT_TABS_TITLE3="tab 3"
DEFAULT_TABS_CMD1="cd /etc"
DEFAULT_TABS_CMD2="cd ~/Documents"
DEFAULT_TABS_CMD3="cd '$HOME/temp/test folder'" # Use quotes like this if there are spaces in the path
open_default_tabs() {
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='$DEFAULT_TABS_TITLE1'; $DEFAULT_TABS_CMD1; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='$DEFAULT_TABS_TITLE2'; $DEFAULT_TABS_CMD2; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='$DEFAULT_TABS_TITLE3'; $DEFAULT_TABS_CMD3; exec bash;"
}
# If length of this is NONzero
if [[ -n "$OPEN_DEFAULT_TABS" ]]; then
OPEN_DEFAULT_TABS= # reset to an empty string so this only happens ONCE
open_default_tabs
exit 0 # close the calling process so only the "default tabs" are left open
fi
2nd, call the open_default_tabs
function from any terminal.
Since you just updated your ~/.bashrc file, you need to let your terminal know about it before it will give you access to your new functions and features. You need to "re-source" your ~/.bashrc file. So, close and reopen your terminal, OR call . ~/.bashrc
or source ~/.bashrc
to re-source your ~/.bashrc file. Then, simply call open_default_tabs
and you'll magically get all your tabs you want opened and titled and cd
ed into the directories you set, like this!
3rd (Optional), create a script and desktop file so you can just double-click the desktop file to load these tabs.
open_tabs.sh:
# Export this variable so your ~/.bashrc file will see it and do the magic.
export OPEN_DEFAULT_TABS=true
# Open a new terminal window, which by default also sources your ~/.bashrc file again,
# thereby kicking off the process since you set the `OPEN_DEFAULT_TABS` variable just above.
gnome-terminal
OPEN_DEFAULT_TABS= # set this variable back to an empty string so it's no longer in force
unset OPEN_DEFAULT_TABS # unexport it
open_tabs.desktop:
[Desktop Entry]
Name=Open Tabs
Name[en_US]=Open Tabs
Comment=
Exec=/path/to/open_tabs.sh
Icon=terminal
Terminal=false
Type=Application
StartupNotify=true
Then do:
chmod +x open_tabs.sh
chmod +x open_tabs.desktop
Place open_tabs.desktop on your desktop and double-click it.
Voila! It's magical! You'll get a new terminal window with 3 titled tabs, each in the proper directory you set based on the commands you configured in your ~/.bashrc file.
Download this and more:
Note that this code, as well as many more useful configuration settings and scripts, have been placed into my dotfile project: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles. This code is here:
- "eRCaGuy_dotfiles/home/README.md" readme
- "~/.bash_aliases" file
- "~/.bash_aliases_private" file
- "open_programming_tools.sh" script
- "open_programming_tools.desktop" desktop launcher file
References:
1. My pleas for help in the process of creating the "New code" version:
- bash: "command not found" when calling function defined in ~/.bashrc file in `bash -c` command while opening gnome-terminal tab
- Open terminal with multiple tabs and execute application which uniquely modifies PS1 variable for each tab
2. Others:
- https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
- https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
- How to run a script without closing the terminal?
- https://stackoverflow.com/questions/16618071/can-i-export-a-variable-to-the-environment-from-a-bash-script-without-sourcing-i
- https://www.shellscript.sh/variables1.html
Responding to comments below
To @egmont:
To answer your question: here's what I get when I do this: gnome-terminal --tab --title abc -e 'sleep 10' --tab --title def -e 'sleep 10'
.
$ gnome-terminal --tab --title abc -e 'sleep 10' --tab --title def -e 'sleep 10'
# 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.
# 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.
HOWEVER, IT DOES OPEN UP 2 NEW TABS INSTANTLY, with titles set to abc and def. After ~10 seconds, however, the tabs auto-close and do NOT remain open.