1

New to bash scripting and looking to tidy my screen. I have a number of terminal windows running separate process but all with the same window title. Using the following i can change the title.. all well and good:

PS1='\[\e]0;TEST TITLE HERE: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$'

If i place this inside a script, it has no effect:

#! /bin/bash
PS1='\[\e]0;TEST TITLE HERE: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$'

What element of scripting have i failed to appreciate that prevents me from doing what I'm trying to do?

many thanks

5
  • 4
    askubuntu.com/q/53177/380067 - In short, you can't affect the parent shell's environment from a subshell; you can fix this by running your script as . script.sh or source script.sh instead of ./script.sh
    – kos
    Apr 19 at 21:35
  • Although the man page was never changed, gnome-terminal has not allowed the -t title business for awhile. xlogo still allows that however if you want to see how things used to work under X.
    – ubfan1
    Apr 19 at 23:40
  • @ubfan1 What's the problem with gnome-terminal's -t option or the manual? The man page says "Set the initial terminal title" and that's exactly what the option does: sets the initial title. Works exactly as documented.
    – egmont
    Apr 20 at 7:24
  • Nope. gnome-terminal -t Fails will pop up a new terminal with the usual title of user@machine:directory The requested title of "Fails" is not displayed. Probably some change to the gnome-terminal python script changed things, and the docs never caught up. @egmont Ubuntu 22.04 and 24.04 behavior -- what system are you running? (the man page of -t=TITLE with the = sign is simply a syntax error)
    – ubfan1
    Apr 20 at 15:57
  • The shell, immediately after starting up, changes the title. The initial one is so short-lived that you probably don't see it. But it's not gnome-terminal's fault, it cannot know that it will be changed so soon. Change your shell prompt not to update the title, or launch a custom command, you will see the initial title working.
    – egmont
    Apr 20 at 17:36

1 Answer 1

6

Now the OP has a lot of things going on in his PS1, below I have removed the major portion of it, to create as simple text as possible - with a solution to the actual problem.

Using a bash FUNCTION you can get the same effect
as from using a script (source script.sh).
$ man bash, then type in /^FUNCTIONS, for info on bash functions.

Example,
place this in .bash_aliases

function setps1 {
PS1="\[\e]0;$1 \a\]$ "
}

Then every bash has the setps1 function available... so
$ type -a setps1 will print

setps1 is a function
setps1 () 
{ 
    PS1="\[\e]0;$1 \a\]$ "
}

and
$ setps1 "Working in \\w"
... will indeed set the window title to "Working in <cwd>"
i.e. Working in Projects while you're cd'd into ...anything/Projects/,
following along as you move about
.

8
  • 1
    You probably want to extend that to something like function setps1 { PS1="\[\e]0;$1 \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "; } to also preserve the original prompt while setting the title ... I think.
    – Raffa
    Apr 20 at 7:28
  • 2
    I intentionally left out that portion of OP's prompt to make it clear how the title-setting works.
    – Hannu
    Apr 20 at 7:29
  • 3
    (i.e. leave out all but the essentials)
    – Hannu
    Apr 20 at 7:31
  • 1
    Now it should be clearer regarding that...
    – Hannu
    Apr 20 at 7:36
  • 1
    @Raffa this is exactly why i had all the noise on my original post; i'd already fallen into that trap :D
    – Paul Eden
    Apr 20 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .