LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2012, 05:58 PM   #1
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Rep: Reputation: 17
[bash] xterm title with ~ shortcut


I am trying to set my xterm title in .bashrc.

This works fine:
Code:
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
However, I'd prefer to see ~ for my home directory.

According to this HowTo, this should do the trick:

Code:
PS1="\[\033]0;\u@\h: \w\007\]bash\\$ "
But it doesn't set my xterm title.

I have also seen another similar tip:

Code:
export PS1=$PS1"\[\e]0;\H:\w\a\]"

I use xfce4-terminal and bash 4.2. (It doesn't work with xterm either.)

Any idea why this isn't working?
 
Old 02-09-2012, 08:33 PM   #2
DucQuoc
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Rep: Reputation: Disabled
From my terminal, it shows "\W" instead of "\w" .
Code:
$ echo $PS1
\u@\h: \W]\$
If you are using Ubuntu , check whether your "sh" link to "bash" or "dash" .

--Duc
 
Old 02-10-2012, 06:54 AM   #3
hashbang#!
Member
 
Registered: Aug 2009
Location: soon to be independent Scotland
Distribution: Debian
Posts: 120

Original Poster
Rep: Reputation: 17
DucDoc, thanks for your reply.

I am using bash on Debian.

My PS1, which - like yours - DOES NOT set the terminal title, just the prompt:
(\W displays the current directory as a relative path, \w as an absolute path.)
Code:
echo $PS1
PS1=\u@\h \w \$

You can indeed set the title bar title by inserting the following sequence in PS1:
Code:
PS1='\u@\h:\w\$ '
case $TERM in
xterm*)
    TITLE='\u@\h - \w'
    PS1="\[\e]0;$TITLE\w\a\]$PS1"
    ;; 
[...]
The green part sets the terminal title, the blue part the prompt.

That, as I just realized, also works for me.


However, I am dynamically setting the prompt in order to get a different coloured $ sign depending on the return code of the last command:


The following code dynamically sets the prompt and the title using PROMPT_COMMAND, where the function set_bash_prompt sets the variable $PROMPT:
Code:
PROMPT_COMMAND='set_bash_prompt;echo -ne "\e]0;${HOSTNAME} : ${PWD} (${USER})\a"'
PS1="$PROMPT"
This also works for me, giving me a dynamically coloured prompt and dynamic title. I just don't like the fully qualified path of $PWD.


The following attempt to set the title via PS1 does not work:
Code:
PROMPT_COMMAND='set_bash_prompt'
PS1="\[\e]0;$TITLE\w\a\]$PROMPT"

The contents of PROMPT_COMMAND are executed as a regular Bash command just before Bash displays a prompt. This somehow conflicts with setting the title dynamically via PS1.


Any work-arounds would be greatly appreciated.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Terminal xterm Title is too dynamic (title doesn't change manually) ZeroDaHero Slackware 4 11-04-2012 12:16 PM
Get the title of xterm/gnome-terminal redleafong Linux - Software 6 01-10-2011 06:38 PM
Change title of xterm - Fedora 2.6 sohnir Linux - General 3 12-13-2005 11:40 AM
How to set title in xterm according to what program running ? tvn Linux - General 1 07-25-2004 07:58 PM
custom xterm title when already defined in .bashrc brian0918 Linux - Software 1 06-01-2004 11:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration