Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a debug trap that runs every time I enter a command in bash that sets the window title to indicate what command is running. I'm leaving out all the configuration details and boil it down to:

export PS1="\[\e]0;$GENERATED_WINDOW_TITLE\a\]$GENERATED_PROMPT"

This works incredibly well, with only one snag: if the bash shell is running in an environment that does not support this feature, the GENERATED_WINDOW_TITLE is printed on the screen with each prompt. This happens any time I'm running bash from a non-X terminal.

How can bash tell if this escape sequence is supported?

share|improve this question
    
I'm looking at "tput has_status_line" as a possible starting point, but all my terminals claim that this is not a known terminfo capability - despite documenting it as a boolean in terminfo. – Sniggerfardimungus Feb 26 '13 at 23:37
    
I've updated the blog post on this topic, in case you're interested in the fun color prompt and status line stuff I was doing with this: foodini.org/permalink.cgi?link=20080816 – Sniggerfardimungus Feb 27 '13 at 20:28
up vote 3 down vote accepted

I don't think there's a terminfo capability for that. In practice, testing the value of TERM should be good enough. That's what I do in my .bashrc and .zshrc, and I don't recall it being a problem.

case $TERM in
  (|color(|?))(([Ekx]|dt|(ai|n)x)term|rxvt|screen*)*)
    PS1=$'\e\]0;$GENERATED_WINDOW_TITLE\a'"$PS1"
esac
share|improve this answer
1  
Heh heh. I just plugged in an "if [ $TERM == "xterm" ];" but holy cow! How did you pull that list of possibilities together? – Sniggerfardimungus Feb 26 '13 at 23:44
    
@Sniggerfardimungus Right now, grep -C5 title ~/.zshrc. When I wrote it (last updated 2007)… I didn't leave a comment, so by observation I guess. – Gilles Feb 26 '13 at 23:50
    
Thanks for the hack. I've had trouble getting it to parse, though, and went with a simpler version that I'll build up over time as I find a need for more terminals. – Sniggerfardimungus Feb 27 '13 at 20:29

There is a terminfo entry which is (ab)used for this purpose, and has been the subject of more than one bug report suggesting that it be applied to various terminal descriptions. Referring to terminfo(5):

   has_status_line           hs     hs   has extra status
                                         line
   from_status_line          fsl    fs   return from status
                                         line
   to_status_line            tsl    ts   move to status line,
                                         column #1

these are explained in the section Status Lines:

Some terminals with status lines need special sequences to access the status line. These may be expressed as a string with single parameter tsl which takes the cursor to a given zero-origin column on the status line.

Offhand, the only terminal emulator which you are likely to be able to use which supports the feature as documented is kterm.

An extension would be appropriate. The screen program documents a possible choice (but after inspecting what it does with the feature, that idea was discarded). ncurses provides an extension which has been in the terminal database a few years, documented in the section on XTerm Extensions:

# TS is a string capability which acts like "tsl", but uses no parameter and
#    goes to the first column of the "status line".

Ultimately, anything that uses the feature would inherit from xterm+sl:

# These building-blocks allow access to the X titlebar and icon name as a
# status line.  There are a few problems in using them in entries:
#
# a) tsl should have a parameter to denote the column on which to transfer to
#    the status line.
# b) the "0" code for xterm updates both icon-title and window title.  Some
#    window managers such as twm (and possibly window managers descended from
#    it such as tvtwm, ctwm, and vtwm) track windows by icon-name. Thus, you
#    don't want to mess with icon-name when using those window managers.
#
# The extension "TS" is preferable, because it does not accept a parameter.
# However, if you are using a non-extended terminfo, "TS" is not visible.

The (ncurses) tput program can test this feature.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.