2

I have found this script to change my current terminal tab title:

#!/usr/bin/env bash
T=$1
ORIG=$PS1
TITLE="\e]2;$T\a"
PS1=${ORIG}${TITLE}
echo "Terminal tab title changed to $T"

It works well if I type them directly in the terminal (with TITLE="\e]2;HELLO\a" for example)

But inside a shell script (rename.sh) $PS1 is empty and the script does not work.

rename.sh HELLO outputs "Terminal tab title changed to HELLO" but the terminal title is not changed. Inside the script $PS1 is empty.

Someone can help me understand this ?

6

The script works by setting the shell's interactive prompt to a string which includes control codes to manipulate the xterm window title. Each time the shell's prompt is displayed, the control codes to change the window title are output.

But of course, inside a script, no interactive prompt is ever displayed, so these commands have no observable effect (though if you started another interactive shell from within the script, you could see the window title change). And because no script can change the environment of its parent process, the change is lost once your script terminates.

Anyway, from your script, you could of course print out the control codes directly.

printf '\033]2;Hello\a'

This changes the window's title once, but if any other program later changes it again, your old title will be lost. The trick to change your prompt is widespread because some popular programs in the past would often change your window title soon after you changed it to your liking (though I don't think this is a common problem any longer). The drawback is that if something has a genuine reason to change your window title, that will now be superseded as soon as your shell displays its prompt again.

If you want code to change your current shell's prompt, you can't put those in a regular script; but you can source the script file, or put the commands in a shell function instead (commonly done in your Bash profile in order to make it persistent).

Incidentally, the Bash prompt should include additional control codes to tell Bash when a part of the prompt is effectively zero width, as far as calculating the display width of the prompt is concerned. You will find that line wrapping is erratic if you type a long command and then need to backspace, for example; Bash will attempt to redraw the prompt, but does it in the wrong place, because it thinks the screen control codes contribute to the prompt's width. You'll want to add these \[ and \] Bash control codes around them.

PS1="$ORIG\[$TITLE\]"

(The curly braces aren't really contributing anything, and hamper legibility, so I took them out.)

  • Aliases are also popular and would certainly work here, but they are less versatile than functions, so I hesitate to recommend them for anything. Too many beginners used them extensively and were then upset when they discovered the limitations. – tripleee Apr 14 '17 at 10:00
  • source ~/rename.sh HELLO works ! Thank you ! – MickaelFM Apr 14 '17 at 10:05
  • 1
    The typical reason to set the terminal title from the prompt is to put something that's variable, such as the current directory, the status of the previous command, the current command (needs to be set from a different hook, not from the prompt), etc. – Gilles Apr 14 '17 at 22:18
1

The variable PS1 is being used only in interactive shell, to access it you need to start your script with bash -i rename.sh.

Minimal example:

$ echo 'echo $PS1' > test

then compare

$ bash test
$ bash -i test

To run the script directly as an executable, add the following to the initial line

#!/bin/bash -i

Notice this works only if you chmod and run the script directly (e.g. ./rename.sh), and not with bash rename.sh, otherwise you'll have to call bash -i rename.sh again.

Edit

To change the terminal title directly, you can echo the control codes directly. Try

echo -e '\033]2;SomeTitle\007'

or, inside a script, with variables

title="SomeTitle"
echo -e '\033]2;'$title'\007'
  • The test work with -i. But adding -i on the shebang line in my script (executable) does not work. No error is outputted, it just does not work as before – MickaelFM Apr 14 '17 at 10:00
  • @MickaelFM I know, sorry if I wasn't clear enough, I was referring to the inside a shell script (rename.sh) $PS1 is empty part, for the title specifically, see the other answer. – resc Apr 14 '17 at 10:04
  • @MickaelFM Added the title part, can you try it? – resc Apr 14 '17 at 10:10
  • 1
    Nope, echo -e '\033]2;SomeTitle\007' does not work even in the terminal directly – MickaelFM Apr 14 '17 at 10:12

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.