Tux

...making Linux just a little more fun!

2-cent Tips: Same prompt different term

Andrew Elian [a_elian at sympatico.ca]
Wed, 25 Oct 2006 14:18:37 -0400

Hello.

Here's a quick tidbit to help the PS1 variable do the right thing depending on the terminal - X or otherwise. I've added these lines to my .bash_profile and found them useful:

case $TERM in
        xterm)
                export TERM=xterm-color
                export PROMPT_COMMAND='echo -ne "\033]0;${USER}:${PWD/#$HOME/~}\007"'
                export PS1="$ "
        ;;

        rxvt|Eterm)
                export PROMPT_COMMAND='echo -ne "\033]0;${USER}:${PWD/#$HOME/~}\007"'
                export PS1="$ "
        ;;

        linux)
                export PS1="\[\033[0;32m\]\u \[\033[1;32m\]\W]\[\033[0;32m\] "
        ;;
esac

Sincerely, Andrew


Top    Back


Martin Hooper [martinjh at blueyonder.co.uk]
Wed, 25 Oct 2006 20:34:23 +0100

Andrew Elian wrote:

> Hello.
> 
> Here's a quick tidbit to help the PS1 variable do the right thing
> depending on the terminal - X or otherwise.  I've added these lines to
> my .bash_profile and found them useful:

On the same note how would you find out wether you were logged in as root or not?

Basically I want to do the same as Andrew but based on what the user was logged in as.


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]
Wed, 25 Oct 2006 20:42:57 +0100

On Wed, Oct 25, 2006 at 08:34:23PM +0100, Martin Hooper wrote:

> Andrew Elian wrote:
> > Hello.
> > 
> > Here's a quick tidbit to help the PS1 variable do the right thing
> > depending on the terminal - X or otherwise.  I've added these lines to
> > my .bash_profile and found them useful:
> 
> On the same note how would you find out wether you were logged in 
> as root or not?
> 
> Basically I want to do the same as Andrew but based on what the 
> user was logged in as.
[ "$(id -u)" == 0 ] &&
{
    # Do stuff as root
} || {
  # Do stuff as normal user
}

-- Thomas Adam

-- 
"Wanting to feel; to know what is real.  Living is a lie." -- Purpoise
Song, by The Monkees.


Top    Back


Benjamin A. Okopnik [ben at linuxgazette.net]
Wed, 25 Oct 2006 16:56:36 -0400

On Wed, Oct 25, 2006 at 08:34:23PM +0100, Martin Hooper wrote:

> 
> On the same note how would you find out wether you were logged in 
> as root or not?
> 
> Basically I want to do the same as Andrew but based on what the 
> user was logged in as.

I set my prompt to a different color based on the above.

    case $UID in
	    0) export PS1='\[^[[40;31m\]\u@\h:\w\$ ' ;;
	    *) export PS1='\[^[[40;0m\]\u@\h:\w\$ ' ;;
    esac

That little octothorpe just isn't enough of a warning. :)

* Ben Okopnik * Editor-in-Chief, Linux Gazette * https://LinuxGazette.NET *


Top    Back


Benjamin A. Okopnik [ben at linuxgazette.net]
Wed, 25 Oct 2006 17:04:18 -0400

On Wed, Oct 25, 2006 at 02:18:37PM -0400, Andrew Elian wrote:

> Hello.
> 
> Here's a quick tidbit to help the PS1 variable do the right thing
> depending on the terminal - X or otherwise.  I've added these lines to
> my .bash_profile and found them useful:
> 
> case $TERM in
> 
>         xterm)
>                 export TERM=xterm-color
>                 export PROMPT_COMMAND='echo -ne "\033]0;${USER}:${PWD/#$HOME/~}\007"'
>                 export PS1="$ "
>         ;;
> 
>         rxvt|Eterm)
>                 export PROMPT_COMMAND='echo -ne "\033]0;${USER}:${PWD/#$HOME/~}\007"'
>                 export PS1="$ "
>         ;;
> 
>         linux)
>                 export PS1="\[\033[0;32m\]\u \[\033[1;32m\]\W]\[\033[0;32m\] "
>         ;;
> esac

Nice! That should come in useful for anyone using different types. Fortunately, they all use the same 'xterm*' entries in the .Xresources file, so other bits of behavior, etc. don't need to be adapted.

Somewhat in the same vein, I like to distinguish between consoles 1-3 by setting different colors (I also use red, but it's reserved for root logins). From my ~/.bash_profile:

TTY=`tty`
[ ${TTY:5:3} == "tty" ] && {             # If not a console, bail!
    color=(x blue green magenta)         # tty's start at 1, arrays at 0...
    setterm -foreground ${color[${TTY#*y}]} -store
}
* Ben Okopnik * Editor-in-Chief, Linux Gazette * https://LinuxGazette.NET *


Top    Back