I was looking for a solution to auto set custom title in the terminal based on what command is being run on the current terminal. So, rather than java showing in many tabs - I'd love to have it pick apart which java program is running not that all say just java.

I found very interesting and different answers yet not fully useful for my requierment

currently, I have written below line in ~/.bash_profile

trap 'printf "\033]0;%s\007" "${BASH_COMMAND//[^[:print:]]/}"' DEBUG

which works somewhat fine in **iTerm2 ** but it displays the complete command as a title but I want the specific argument or part of the string of command as a title

suppose I run this command in the terminal

sudo java - jar xyz_service/build/lib/xxx_snapshot.jar

and the terminal title being set as it is but I only want to set xyz_service as a title

note: above command always display update_terminal_cwd as a title in default bash shell terminal

my other trials which are not working though

if [ "$SHELL" = '/bin/bash' ]
    case $TERM in
            set -o functrace
            trap 'echo -ne "\e]0;"; echo -n $BASH_COMMAND; echo -ne "\007"' DEBUG
            export PS1="\e]0;$TERM\007$PS1"

this is also not working

settitle () {
    echo -ne "\033]0;${PREV_COMMAND}\007"
    export PREV_COMMAND=${PREV_COMMAND}' | '


My environment is:

  • macOS HighSierra v 10.13.3
  • iTerm2 build 2.15
  • shell is bash v 2.8 (400)

Can I do this without a wrapper alias in bash that explicitly sets the variable name?

You must log in to answer this question.

Browse other questions tagged .