2

I've got a simple command that sets the term title:

# Allow the user to set the title.
function title {
   PROMPT_COMMAND="echo -ne \"\033]0;$1 ($PWD)\007\""
}
export -f title

I'd like to access this from a script:

16:28 $ cat ./scripts/webserver.sh
#!/bin/bash
title 'webserver'
nodemon ./app/webserver.coffee 

I've placed title in .bashrc, .bash_profile and .profile and sourced every one of those files after. I can run the title function fine from the command line. Regardless of what I do, the title function doesn't run when I run my script file. There's no error, just no change to the title. What am I doing wrong?

A bit more info: I added an echo 'changing title' line in the title function. The echo is being output but the title isn't being changed. So there must be a problem with the title function?

Related: How to change Gnome-Terminal title?

jcollum
  • 1,032

1 Answers1

0

If you need to change the title of the terminal, I'd advise you to use wmctrl that will also work from within a script (in fact, your solution fails because the prompt is not used when running scripts). So, first install the wmctrl package, e.g., using apt:

sudo apt-get install wmctrl

The nice thin with bash is that it has a variable named WINDOWID that contains a window id (no kidding!) that wmctrl can use. From within Bash or from within a script, try:

wmctrl -ir $WINDOWID -N "A cool title"
  • The -i option is to tell wmctrl that the window will be specified using its numerical id (do echo $WINDOWID to check that WINDOWID is indeed a number, and do wmctrl -l to list all the windows managed by your window manager and compare... you'll probably have to convert from decimal to hexadecimal or maybe use printf '%#.8x\n' "$WINDOWID").
  • The -r option is to tell wmctrl that the specified window will be ready for the following action, i.e., -N, change its title to the specified title.

man wmctrl for more info about this funny little tool.

Enjoy!