Ubuntu: Command line prompt looks strange in virtual console



Question:

In a terminal emulator like xterm my prompt looks like this:

[august@macmini ~]$  

but in a virtual console the string ;linux is prepended to the prompt to make it look like this:

;linux[august@macmini ~]$  

Does anyone know where the "linux" string may come from?


Solution:1

It turns out that things go wrong when a command to set the window title is executed in a virtual console (where there is no window). Changing

PROMPT_COMMAND='printf "\e]0;%s\a" "$TERM"'  

to

if [[ $TERM = xterm* ]]; then      PROMPT_COMMAND='printf "\e]0;%s\a" "$TERM"'  fi  

solves the problem.


Solution:2

Just for fun, here's how I set my prompt:

# inspiration: http://www.stumbleupon.com/su/2LpQMi   user_host_path="${debian_chroot:+($debian_chroot) }"'\u@\h: \w'  xterm_title='\[\e]0;'"$user_host_path"'\a\]'  color_bold='\[\033[0;1m\]'  color_reset='\[\033[0m\]'  [[ $TERM == xterm* || $TERM == rxvt* ]] && first_part="${xterm_title}"  first_part+="\n${user_host_path}\n"  last_part='\! \$ '  PROMPT_COMMAND="((\$? != 0)) && PS1='${first_part}${color_bold}[\$?]${color_reset} ${last_part}' || PS1='${first_part}${last_part}'"  unset user_host_path xterm_title color_bold color_reset first_part last_part  

Looks like this:

glennj@homebase: ~  200 $ (exit 42)    glennj@homebase: ~  [42] 201 $ cd /tmp    glennj@homebase: /tmp  202 $   


Solution:3

I wasn't crazy about the solution provided here since I use the same TERM for Ubuntu virtual terminals as other terminals (screen-256color). Thus, I took a quick look at what other environment variables could be useful for this purpose.

My conclusion was to use the XDG_VTNR (XDG virtual terminal number?) variable. Since Ubuntu uses VT#7 for the desktop environment, a simple check for whether this variable is greater than or equal to 7 does the trick.

I have bash function that builds my prompt anyway, so I just surrounded the title escape sequence addition with a check against the XDG_VTNR variable.

For example, note the bolded portion below:

  # Use new test (i.e. [[ ]]) so as not to fail if XDG_VTNR does not exist  if [[ $XDG_VTNR -ge 7 ]]; then    # Add the window title escape sequence to the prompt  else    # Don't add the window title escape sequence to the prompt  fi    

Just wanted to add this note in case it can be useful for anyone else.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »