LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Enterprise (https://www.linuxquestions.org/questions/linux-enterprise-47/)
-   -   Command Prompt colors (https://www.linuxquestions.org/questions/linux-enterprise-47/command-prompt-colors-936765/)

saeedamer 03-27-2012 05:43 PM

Command Prompt colors
 
Hi there

I am using Oracle Enterprise Linux 4.7 (and some servers have 5.x and 6.x but I guess it does not matter).

I want to be able to display some information in the unix command prompt in colors. I googled and tried this but it does not work:

Code:

export PS1='^[[0;32m^[[1m${HOST}:^[[1;33m${PWD}:^[[0;32m^[[m${ORACLE_SID}^[[0m> '
It just shows the whole string that is in quotes above (except for the env variables - it shows their value). For example, this is what I see:

Code:

^[[0;32m^[[1m:^[[1;33m/export/home/oracle:^[[0;32m^[[1mdevdb1^[[0m>

Could someone please help me with thisÉ

Thanks so much!

lithos 03-27-2012 05:58 PM

Hi,

you should replace single ' quotes with double quotes ".
Code:

PS1="${USER}@${HOST}:"'${PWD}'"> "

good luck.

saeedamer 03-27-2012 06:02 PM

Thank you for your response. While I was waiting, I tried this:


Code:

Green="\033[32m"
Red="\033[31m"
Yellow="\033[33m"
Blue="\033[34m"
Bold=$(tput smso)
Normal=$(tput sgr0)
export PS1="[$Green\u@$Blue$(uname -n)]:$Normal\$PWD: [${Bold}${Red}\${ORACLE_SID}${Normal}] > "

Now it does show me some colors but what happens is when I press UP arrow (to see previous commands) it starts to scroll UP and shows the command on the line above the prompt line and then if i press up arrow again, it just keeps going up.

How to fix that please? I am using bash shell.

lithos 03-27-2012 06:11 PM

It's because the codes are not closed correctly.

Try this prompt:
Code:

PS1="(\[\e[33;1;44m\]$WINDOW\[\e[0m\]) [\H:\[\e[33;1;40m\]\u@\[\e[32;1m\]\w\[\e[0m\]] $\[\e[0m\]"
or try adding to the end of your line this :
Code:

\[\e[0m\]
It should "close" all the codes previously used,

and then modify it to suit yours.

You should check here
see that you need to set:
Code:

tput rmso

    Exit standout mode

which you don't use and maybe some others.

saeedamer 03-27-2012 06:16 PM

Thanks - how to close the color codes?

I tried your provided code and "(\[\e[33;1;44m\]$WINDOW\[\e[0m\]) " only displays "()". Looks like $WINDOW is not defined.

Could you please fix the one I posted? Or let me know how to close the color codes?

Thanks a bunch!

saeedamer 03-27-2012 06:19 PM

Quote:

Originally Posted by lithos (Post 4638136)
It's because the codes are not closed correctly.

Try this prompt:
Code:

PS1="(\[\e[33;1;44m\]$WINDOW\[\e[0m\]) [\H:\[\e[33;1;40m\]\u@\[\e[32;1m\]\w\[\e[0m\]] $\[\e[0m\]"
or try adding to the end of your line this :
Code:

\[\e[0m\]
It should "close" all the codes previously used,

and then modify it to suit yours.

You should check here
see that you need to set:
Code:

tput rmso

    Exit standout mode

which you don't use and maybe some others.

I tried

PS1="[$Green\u@$Blue$(uname -n)]:$Normal\$PWD: [${Bold}${Red}\${ORACLE_SID}${Normal}]\[\e[0m\] > "

but no difference - UP arrows still misbehaves.

lithos 03-27-2012 06:26 PM

I would try yours like this:
Code:

add
NORMALE=$(tput rmso)

export PS1="[$Green\u@$Blue \H]:$Normal \w: [${Bold}${Red}\${ORACLE_SID} $Normal $NORMALE] > "

It's pretty close to working I think, just not exactly what you want.

saeedamer 03-27-2012 06:29 PM

Quote:

Originally Posted by lithos (Post 4638136)
see that you need to set:
Code:

tput rmso

    Exit standout mode

which you don't use and maybe some others.

I tried this but still UP arrow does the same:

Code:

Green="\033[32m"
Red="\033[31m"
Yellow="\033[33m"
Blue="\033[34m"
BoldON="\033[1m"
BoldOFF="\033[22m"
NC="\033[0m" # No Color
Bold=$(tput smso)
#Normal=$(tput sgr0)
Normal=$(tput rmso)
export PS1="[$Green\u@$Blue$(uname -n)]:$Normal\$PWD: [${Bold}${Red}\${ORACLE_SID}${Normal}] > "

Thanks for your help and time.

lithos 03-27-2012 06:30 PM

Have you exited the shell and reconnect/relogin ?

Are you maybe looking at this guide here ?

saeedamer 03-27-2012 06:32 PM

Quote:

Originally Posted by lithos (Post 4638149)
I would try yours like this:
Code:

add
NORMALE=$(tput rmso)

export PS1="[$Green\u@$Blue \H]:$Normal \w: [${Bold}${Red}\${ORACLE_SID} $Normal $NORMALE] > "

It's pretty close to working I think, just not exactly what you want.

Thank you again - what is the fix for UP arrow behavior? It takes me to upper line (while still on unix prompt) if the previous command issued was longer one (does not fit on one line)? I hope you understand what I am trying to say :-(

lithos 03-27-2012 06:34 PM

Huh,

I understand you now, but I'm sorry I don't have answer.

saeedamer 03-27-2012 06:34 PM

Quote:

Originally Posted by lithos (Post 4638156)
Have you exited the shell and reconnect/relogin ?

Are you maybe looking at this guide here ?

thanks for the URL - it is great. Now the color part is OK, it is the UP arrow thing that is bothering me now. Any ideas?

lithos 03-27-2012 06:36 PM

Quote:

Originally Posted by saeedamer (Post 4638141)

I tried your provided code and "(\[\e[33;1;44m\]$WINDOW\[\e[0m\]) " only displays "()". Looks like $WINDOW is not defined.

Yes, WINDOW is a variable set by Screen.

saeedamer 03-27-2012 06:37 PM

Quote:

Originally Posted by lithos (Post 4638160)
Huh,

I understand you now, but I'm sorry I don't have answer.

oh :-(

lithos 03-27-2012 06:44 PM

I tried to look at some solutions,
like here (at the bottom)
Quote:

I had the same issue, and I don't think the problem has to do with the TERM variable. In fact, I tried changing it to "ansi" myself, and emacs got all screwed up.

You didn't put the value of PS1 in your original post, so I can only assume you're having the same issues I was, which was failing to mark the ANSI escape sequences as non-printing characters.

Before I had the following value for PS1:

'\e[0;34m\h:\w [\!]\$\e[0m '

which gave me a nice blue prompt of the following form

hostname:working-directory [command-number]$

However, I had the same line-wrapping problem you did. The fix was to insert \[ and \] around the ANSI escapes so that the shell knows not to include them in the line wrapping calculation. This results in the following value for PS1:

'\[\e[0;34m\]\h:\w [\!]\$\[\e[m\] '
then here

and here

I hope it helps.


All times are GMT -5. The time now is 02:54 PM.