You are not logged in.

#1 2003-10-11 06:55:47

jlowell
Member
Registered: 2003-08-10
Posts: 270

Colorizing Bash Prompt

I've recently installed Gyroplast's patch to colorize the end of the boot process. Encouraged by success in this instance, I'm at work attempting to colorize my bash prompt and have run into an odd problem with the scripting which I don't know how to resolve.

The operant file, of course, is ~/.bashrc. Here's the default with one addition I made to startx into a black background with a white "X" just before it resolves into my window manager's backdrop:

alias ls='ls --color=auto'
PS1='[u@h W]$ '
export PATH=$PATH:/usr/X11R6/bin
alias startx='startx -- -br'

The default PS1= line produces a prompt with the following appearance:

[root@Arch1 root]#

Attempting to colorize the prompt after consulting the relevant LDP Bash How-To, I changed the PS1= line hoping to produce the same prompt in bold red:

PS1="[033[1;31m][u@h W]$[33[0m]"

This arrangement produces a bold red prompt alright but with a somewhat different configuration than I'd expected:

[root@Arch1 root]$

The "$" has replaced the "#" for a reason that I fail to grasp. I've logged in as root! Placing ' marks around [u@h W]$ in the fashion of the default only succeeds in the marks themselves being printed out, hardly the result being sought.

What do I need to do with the new PS1 line to get it to produce the default prompt yet colorize it? It seems strange to me that simply introducing it into an ASCII sequence pulls it off target in this way. Why would this change occur? Thoughts?

jlowell

Offline

#2 2003-10-11 08:09:05

jlowell
Member
Registered: 2003-08-10
Posts: 270

Re: Colorizing Bash Prompt

I just figured things out!

Changing

PS1="[033[1;31m][u@h W]$[33[0m]"

to

PS1='[033[1;31m][u@h W]$[33[0m] '

in the fashion of the default produces the desired result.

If they'd offered it then (which they didn't) and, instead of history, I'd majored in computer science back in 1958-62, I'd have known how to do this right the first time. Might someone suggest a good bash manual so that I might learn decent scripting and in an organized way?

jlowell

Offline

#3 2003-10-11 09:07:25

Xentac
Forum Fellow
From: Victoria, BC
Registered: 2003-01-17
Posts: 1,797
Website

Re: Colorizing Bash Prompt

The reason in this case is the difference in '' compared to "".  "" evaluates the string, whereas '' don't.


I have discovered that all of mans unhappiness derives from only one source, not being able to sit quietly in a room
- Blaise Pascal

Offline

#4 2003-10-11 16:07:26

jlowell
Member
Registered: 2003-08-10
Posts: 270

Re: Colorizing Bash Prompt

Xentac,

Thanks for the explanation. A lot of this is just monkey-see-monkey-do for me right now; there's no logic behind it. I wish I understood more of the "why" of scriping. I've done some DOS batch file writing in the past, but that's of very little help here, although the ACSII color codes themselves were something I'd used before.

Again, thanks.

jlowell

Offline

#5 2003-10-11 17:33:26

zen_guerrilla
Member
From: Greece
Registered: 2002-12-22
Posts: 259

Re: Colorizing Bash Prompt

Hello,
I thought this might be helpful. At work I use these bash prompts :

for root :
PS1='[33[01;33m][`date +'%H:%M'`] [33[01;31m]h [33[01;34m]W $ [33[00m]'

for non-root :
PS1='[33[01;33m][`date +'%H:%M'`] [33[01;32m]u@h [33[01;34m]W $ [33[00m]'

both print time on the prompt (quite handy, since I never wear watches smile) & use colors...

Offline

#6 2003-10-16 16:50:06

pmv
Member
From: Helsinki, Finland
Registered: 2003-09-30
Posts: 22

Re: Colorizing Bash Prompt

zen_guerrilla wrote:

Hello,
I thought this might be helpful. At work I use these bash prompts :

for root :
PS1='[33[01;33m][`date +'%H:%M'`] [33[01;31m]h [33[01;34m]W $ [33[00m]'

for non-root :
PS1='[33[01;33m][`date +'%H:%M'`] [33[01;32m]u@h [33[01;34m]W $ [33[00m]'

both print time on the prompt (quite handy, since I never wear watches smile) & use colors...

Nice, but bit too colorfull for me, but gave me an idea to use colors for warning that I'm root to prevent accidents.

Here's what I use now if someone's intrested:

Root:
PS1='[`date +'%H:%M'`][[33[01;31m]u[33[00m]@h W]$ '

User:
PS1='[`date +'%H:%M'`][u@h W]$ '

Offline

Board footer

Powered by FluxBB