Prompt not hard returning after PS1 variable change?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Prompt not hard returning after PS1 variable change?
I created this script for my .bashrc file, to reshape my prompt:
Code:
# User specific aliases and functions
#Colors:
#Black 0;30 Dark Gray 1;30
#Blue 0;34 Light Blue 1;34
#Green 0;32 Light Green 1;32
#Cyan 0;36 Light Cyan 1;36
#Red 0;31 Light Red 1;31
#Purple 0;35 Light Purple 1;35
#Brown 0;33 Yellow 1;33
#Light Gray 0;37 White 1;37
PROMPTCOLOR='\e[1;37m'
COMMANDCOLOR='\e[0;37m'
USERNAMECOLOR='\e[1;32m'
PS1="${USERNAMECOLOR}\u${PROMPTCOLOR} [\w] >${COMMANDCOLOR} "
It works well, but I apparently have a side-effect, in that if I type a command longer than the width of the screen, it seems to fail to do a hard return, overwriting the current line. What did I mess up?
P.S. I really like this script. It works well for me. I set up my root script so it shows in red, which reminds me of my current user state.
I've narrowed it down to the color variables, and not the PS1 prompt directly. I removed all references to the color variables from the PS1 variable, and the problem went away. I tried each color variable in turn, and the problem returns. I also tried adding the ;00 (null) to the prompt, but that doesn't do anything helpful.
I really like my prompt configuration, but I would like to get rid of this one side-effect.
Ok, I'll admit it, I feel good. I figured it out ... all by myself, and without y'all's help!
I figured out that bash determines the text wrap based on the length of the PS1 variable. So I had to add \[ and \] to nonvisible/special codes, which included the color codes.
This is my code so far, and I plan to extend it further:
Code:
#Color Codes:
#Black 0;30 Dark Gray 1;30
#Red 0;31 Light Red 1;31
#Green 0;32 Light Green 1;32
#Brown 0;33 Yellow 1;33
#Blue 0;34 Light Blue 1;34
#Purple 0;35 Light Purple 1;35
#Cyan 0;36 Light Cyan 1;36
#Light Gray 0;37 White 1;37
PROMPTCOLOR='\[\e[1;37m\]'
COMMANDCOLOR='\[\e[0;37m\]'
ROOTCOLOR='\[\e[1;31m\]'
NORMALUSERCOLOR='\[\e[1;32m\]'
HOSTNAMECOLOR='\[\e[1;35m\]'
DIRECTORYCOLOR='\[\e[1;33m\]'
### username ###
if [ 'whoami' = 'root' ]
then USERCOLOR=$ROOTCOLOR
else USERCOLOR=$NORMALUSERCOLOR
fi
PS1=$USERCOLOR"\u"
### hostname ###
PS1=$PS1$PROMPTCOLOR@
PS1=$PS1$HOSTNAMECOLOR$(hostname -s)
### working directory ###
PS1=$PS1$PROMPTCOLOR" ["
PS1=$PS1$DIRECTORYCOLOR"\w"
PS1=$PS1$PROMPTCOLOR"]"
### prompt end marker ###
PS1=$PS1$USERCOLOR" \$ "
### command color ###
PS1=$PS1$COMMANDCOLOR
I would be happy to receive positive criticism on my script, whether to tell me how to make it better, or to tell me how much you like it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.