1

What I'm trying to do:

  1. Write a script to open 3 tabs.
  2. cd into a different folder in each tab (ie: run a unique command).
  3. 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 exporting and sourceing and just don't know what I'm doing wrong here.

Related:

  1. Open terminal with multiple tabs and execute application which uniquely modifies PS1 variable for each tab
  2. https://unix.stackexchange.com/questions/177572/how-to-rename-terminal-tab-title-in-gnome-terminal/566383#566383
  3. 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.
    
2

To get bash to read and execute ~/.bashrc on startup, start it as an interactive shell:

gnome-terminal --tab -- bash -ic "set-title hey; exec bash"

Now why didn’t your approach of sourcing the file in a non-interactive shell work? I strongly assume your ~/.bashrc begins with something like this:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

This lets it return without doing anything if the shell where it is sourced doesn’t have “i” anywhere in its $-, i.e. it’s an interactive shell. $- is a special parameter, man bash says:

- Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.