What I'm trying to do:
- Write a script to open 3 tabs.
cd
into a different folder in each tab (ie: run a unique command).- get each tab to have a unique title
I want this scripted so I can click the script on my Desktop and have it open up terminals as I'd like for my daily development environment.
Description:
I have this script to try and open 3 terminal tabs with unique commands to be run in the tabs:
open_tabs.sh
#!/bin/bash
gnome-terminal --tab -- bash -c "source $HOME/.bashrc && set-title hey; exec bash"
gnome-terminal --tab -- bash -c "cd ~; exec bash"
gnome-terminal --tab
When I run it with ./open_tabs.sh
, it opens up 3 new tabs, but unfortunately set-title
doesn't work to set the tab title! I get this error in that tab that opens:
bash: set-title: command not found
I have set-title
defined as a function in ~/.bashrc
like this. Its purpose is to set the title string at the top of any terminal window. It works perfectly when I use it manually. Ex: set-title hey how are you?
will put "hey how are you?" at the top of my terminal window.
# From: https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
set-title() {
# If the length of string stored in variable `PS1_BAK` is zero...
# - See `man test` to know that `-z` means "the length of STRING is zero"
if [[ -z "$PS1_BAK" ]]; then
# Back up your current Bash Prompt String 1 (`PS1`) into a global backup variable `PS1_BAK`
PS1_BAK=$PS1
fi
# Set the title escape sequence string with this format: `\[\e]2;new title\a\]`
# - See: https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
TITLE="\[\e]2;$@\a\]"
# Now append the escaped title string to the end of your original `PS1` string (`PS1_BAK`), and set your
# new `PS1` string to this new value
PS1=${PS1_BAK}${TITLE}
}
How do I fix this!? I've tried export
ing and source
ing and just don't know what I'm doing wrong here.
Related:
- Open terminal with multiple tabs and execute application which uniquely modifies PS1 variable for each tab
- https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
Open Terminal with multiple tabs and execute application <== this is what I'm really trying to solve, but
gnome-terminal
's--command
(-e
) option is now deprecated!# 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.