Set title for each terminal tab in gnome-terminal using a bash script

11,129

If you want to use a function stored in ~/.bashrc then source that file in your command:

gnome-terminal --tab -e "bash -c 'source ~/.bashrc;set-title 99;ping 192.168.7.99'"

You've mentioned in the comments that you plan to use this in a shell script and with multiple gnome-terminal tabs. As a proof of concept, you can use the following script as example:

#!/bin/bash
gnome-terminal --tab -e "bash -c 'printf \"\033]0;TEST1\007\"; sleep 7'" \
               --tab -e "bash -c 'printf \"\033]0;TEST2\007\"; ping -c 4 8.8.8.8'" \

Instead of bash function, this uses printf and escape sequences directly. Please be mindful of the backslashes.

Share:
11,129

Related videos on Youtube

d a i s y
Author by

d a i s y

Updated on September 18, 2022

Comments

  • d a i s y
    d a i s y over 1 year

    I'm using Ubuntu 16.04

    I want to open multiple terminal tabs, run commands and set title. I can open multiple tabs with this command:

    gnome-terminal --tab -e "command1" --tab -e "command2"
    

    but cannot use --title option as it is not available in this version.

    I know mate-terminal can do this, but I want to use gnome-terminal.

    I've applied solution posted here and it worked but when i run

    gnome-terminal --tab -e "bash -c 'set-title 99;ping 192.168.7.99'"
    

    It shows:

    bash: set-title: command not found
    PING 192.168.7.99 (192.168.7.99) 56(84) bytes of data.
    64 bytes from 192.168.7.99: icmp_seq=1 ttl=128 time=0.425 ms
    64 bytes from 192.168.7.99: icmp_seq=2 ttl=128 time=0.353 ms
    64 bytes from 192.168.7.99: icmp_seq=3 ttl=128 time=0.335 ms
    

    I also applied the solution suggested here on Unix & Linux SE

    I've also read this post setting-terminal-tab-titles but the accepted answer did not solve my issue in 16.04 os or gnome-terminal version 3.18.3 and other solution provides to use other terminal xterm and I want to use gnome-terminal.

    • John N
      John N over 7 years
      Possible duplicate of Setting Terminal tab titles
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy over 7 years
      Please note, that the core of the problem was that OP wanted to use a function defined in ~/.bashrc within custom script. Thus, the question is only tangentially related to the link John N provided. IMHO this is not a strict duplicate
  • d a i s y
    d a i s y over 7 years
    It opens terminal, run command but not set title.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 7 years
    @Lucy try this function instead : setTitle() { echo -e "\033]0;$@\007" }
  • d a i s y
    d a i s y over 7 years
    means to put this in .bashrc file & remove that old function?
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 7 years
    @Lucy yes. Or you can put both there, just use different names. I just tested it with my own gnome-terminal, works. imgur.com/a/Dto82
  • d a i s y
    d a i s y over 7 years
    put that line, closed and open terminal. It says bash: /home/lucy/.bashrc: line 120: syntax error: unexpected end of file
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 7 years
    @Lucy interesting. did you put that as the last line ? Try putting it on the top. Also, break it down into 3 lines. I've posted a more-formatted example of the function on the same post that you referenced. See askubuntu.com/a/860497/295286
  • d a i s y
    d a i s y over 7 years
    To use setTitle() { echo -e "\033]0;$@\007" } in .bashrc worked for me
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 7 years
    @Lucy perfect ! Well, congrats , we've solved your question. Also final note: you don't have to use it as function, you probably could simply use echo -e "\033]0;TITLE TEXT\007" in your `gnome-terminal command directly. I will make a small addition to my answer, please see it later. Thx
  • d a i s y
    d a i s y over 7 years
    Yes. Thank you but i have to use it in bash script where title will be used as variable (setTitle $i) and will be opened multiple tab and run command so this is perfect for me.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy over 7 years
    @Lucy perfect ! that means my addition might be helpful to you. Please see it
  • d a i s y
    d a i s y over 7 years
    I think you should also provide the function in answer that worked for me.
  • WillC
    WillC about 7 years
    printf and escape sequences is ugly and error-prone. askubuntu.com/a/774543/455406 works for bash
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 7 years
    @WillC Funny thing is that you said it's error prone, yet you link to answer which also uses escape sequences :) . On the other hand, printf is more portable than echo, because users might not necessarily be using bash as a shell, and each shell has its own echo implementation.
  • WillC
    WillC about 7 years
    Thanks: I wasn't clear, and I was generalising my lack of familiarity with escape codes. I meant that typing a printf inline each time is error-prone for me because I'm not skilled with escape codes. If I create that bash-specific 'set-title' function in my ~/custom_functions.conf, I don't have to re-type any more escape sequences, and so I'm assessing that answer as less error-prone because it reduces the number of escape sequences that I have to type, reducing mis-types that I will make.
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 7 years
    @WillC oh, you're absolutely right - turning a set of commands into functions is very frequent and one of the best practices.