Set screen name from shellscript

Can I set the screen title using a shell script?

I was thinking of something like sending keyboard commands ctrl + A shift - A Name enter

I searched for about an hour how to emulate keystrokes in a shell script, but could not find an answer.

+45
bash xterm gnu-screen title
Nov 06 '09 at 13:37
source share
12 answers

You can set the screen title / xterm using the following lines:

 #!/bin/bash mytitle="Some title" echo -e '\033k'$mytitle'\033\\' 

[UPDATE] - on request I also include the solution suggested by @Espo below:

Depending on your version of xterm or your Linux distribution, the line above may or may not work, and you can try xterm-defaults:

 #!/bin/bash mytitle="Some title" echo -e '\033]2;'$mytitle'\007' 

For more details see: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3 or see @Espo's answer below.

+47
Nov 06 '09 at 13:49
source share

From http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s3

xterm escape sequences

Window and icon titles can be changed in running xterm using XTerm escape sequences. The following sequences are useful in this regard:

  • ESC]0;stringBEL - Set the icon name and window title to a string
  • ESC]1;stringBEL - Set the icon name to a string
  • ESC]2;stringBEL - Set the window title to a string

where ESC is the escape character (\ 033) and BEL is the bell character (\ 007).

Printing one of these sequences inside xterm will bring up a window or the name of the icon you want to change.

Note: these sequences apply to most xterm derivatives, such as nxterm, color-xterm, and rxvt. Other terminal types often use different escape sequences; see the application for examples. For a complete list of xterm escape sequences, see the ctlseq2.txt file that appears with the xterm distribution or xterm.seq that comes with the rxvt distribution.

Printing escape sequences

For information that is constant throughout the life of this shell, such as the host and username, it will be enough just to repeat the string escape in the rc shell file:

  echo -n "\033]0;${USER}@${HOST}\007" 

should create a name of the type username @hostname, provided that the shell variables $ USER and $ HOST are set correctly. The required parameters for the echo may vary depending on the shell (see examples below).

Information that may cover the lifetime of the shell, such as the current working directory, these screens really need to be applied every time the prompt changes. Thus, the line is updated with each command you issue and can track information such as the current work directory, username, host name, etc. Some shells provide special functions for which some do not, and we need to insert header sequences directly into the help line. This is illustrated in the next section.

+19
Nov 06 '09 at 13:49
source share

The following are other script screen renaming methods:

Adding the following parameters to .ssh/config automatically sets the screen title when logging in using SSH:

 Host * PermitLocalCommand yes LocalCommand [ "$TERM" == 'screen' ] && echo -ne "\033k%h\033\\" 

Instead of %h , which represents the host name of the machine you are connecting to, you can use %n , which is the actual name / alias that you used to connect to the machine.

NOTE. To use the Localhost% n and% h parameters, you must use OpenSSH> = v5.1. Check out man ssh_config for more information on LocalCommand.

To automatically return the header back to the localhost host name, after closing the SSH session, you can add an escape sequence to the PS1 invitation variable in .bashrc :

 export PS1='you_favorite_PS1_here' if [ "$TERM" == 'screen' ]; then export PS1=${PS1}'\[\033k\h\033\\\]' fi 

These tricks are especially useful when using the .screenrc configuration, which shows you which tab of the screen you are currently working on. Add the following: .screenrc to get this:

 caption always "%{= kY}%-w%{= Yk}%n %t%{-}%+w%{ kG} %-= @%H - %LD %d %LM - %c" 
+11
Jun 22 2018-12-22T00:
source share

Try the commands below, there is no need to edit any file or configuration, for example ~ / .bashrc. It can be used at runtime.

Set static text as title: (My Title)

 export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 

Set local / global variable as heading: ($ USER)

 export PS1='\[\e]0;$USER\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 

Set the output of the command as the header: (hostname)

 export PS1='\[\e]0;`hostname`\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 

Set the default value (Go back):

 export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 
+10
Mar 20 '14 at 0:52
source share
 set_screen_title () { echo -ne "\ek$1\e\\" } 
+7
Nov 06 '09 at 13:49
source share

You can also call up the screen and tell it to set the title:

 screen -X title "new title" 

If you are in a screen window, it will set this window name. If you are not on the screen, it will set the most recent open window name.

+3
Feb 24 '11 at 10:14
source share

To add Espo to the answer, xterm escape sequences can also be applied to the PS1 Bash variable

 ESC]0;stringBEL -- Set icon name and window title to string ESC]1;stringBEL -- Set icon name to string ESC]2;stringBEL -- Set window title to string 

Example

 PS1='\e]0;string\a' 
+2
May 04 '12 at 22:17
source share

To enable automatic updating of headers when navigating through ssh, add this to ~/.bashrc :

 ssh() { echo -n -e "\033k$1\033\\" /usr/bin/ssh "$@" echo -n -e "\033k'hostname -s'\033\\" } echo -n -e "\033k'hostname -s'\033\\" 

See http://linuxepiphany.blogspot.com.ar/2010/05/good-screenrc-config-setup.html.

+1
08 Feb
source share

The real simple solution (after I tried everything that I have seen so far) ... if you have a system like Ubuntu / Mint where you can install "xtitle":

sudo apt-get install xtitle

after that you just need to use the command: xtitle My title

and you will get: "Terminal is my heading"

+1
May 15 '16 at 5:46 a.m.
source share
  # add the following in your ~/.bashrc or ~/.bash_profile PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' 

or even better, copy the whole concept to configure your bash configurations between multiple hosts from here

0
Jul 28 '17 at 14:36
source share

My solution to this problem was to create a bash script and add it to my ~ / .bashrc file:

 set-title() { ORIG==$PS1 TITLE="\e];$@\a" PS1=${ORIG}${TITLE} } 

Now that I am in any bash shell session, I type “set-title wanted_title” and it changes to “required title”. This works for several versions of Ubuntu, currently on Kinetic 16.04

I got this solution from here . I looked for it again, could not find it and thought that I would post it here for anyone interested.

0
Jan 28 '19 at 17:02
source share

I got this solution by experimenting with others like @ flurin-arner for example. I ran the @ weston-ganger set-title () set . I also used the @ imgx64 PROMPT_DIRTRIM clause . I also use the @itseranga git branch tooltip, although this has nothing to do with the question, which shows what you can do with the tooltip.

First, as shown by Weston and above

  TITLE="\[\e]2;$*\a\]" 

can be used to manually set the terminal header, "$ *" is command line input, but not what we want.

Secondly, as I said, I also add git branch to my tooltip, again not part of the question.

 export PROMPT_DIRTRIM=3 parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' } export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ " 

Thirdly, experimentally I copied the TITLE code above, set $ * to a fixed line and tried this:

 see: \[\e]2;'SomeTitleString'\a\] export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\[\e]2;'SomeTitleString'\a\] $ " 

It had the desired effect! Ultimately, I wanted to use the base path as the title. PS1 Params shows that \ W is the base path, so my solution is this:

 export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\[\e]2;\W\a\] $ " 

without git branch:

 export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\[\033[00m\]\[\e]2;\W\a\] $ " 

the result is a prompt with git-branch:

 user@host ~/.../StudyJava (master) $ 

the result is an invitation without parse_git_branch:

  user@host ~/.../StudyJava $ 

where pwd gives

 /home/user/somedir1/otherdir2/StudyJava 

and terminal name

 StudyJava 

NOTE. Starting with @seff, I essentially replace "My Title" with "\ W"

 export PS1='\[\e]0;My Title\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' 
0
May 22 '19 at 19:20
source share



All Articles