I am new to Bash programming and I am working on creating a Bash command line. My goal is to create an invitation that displays only the login name and hostname if they are different from what I usually use. I also want to add the current Git branch to the command line when it is in a directory under Git's control.
I would like to color the login section and node name section green, the blue path to the directory, the Git branch section is pink, and the delimiters (and $ icons) are white. However , when a previously executed command returns anything other than zero, I would like to color the $ red delimiter . The general format without colors is as follows:
loginname@hostname :~/current/path:branchname$
The only sections that are required are the directory path and the $ character. Here is the code I wrote for my .bashrc file :
MYNAME="gwen" MYHOST="gwen-laptop" RED="\[\033[31m\]" WHITE="\[\033[0m\]" GREEN="\[\033[01;32m\]" BLUE="\[\033[01;34m\]" PINK="\[\033[01;35m\]" DOLLAR="if [ \$? = 0 ]; then echo ${WHITE}\$; else echo ${RED}\$${NORMAL}; fi" GITBRN='$(__git_ps1 "\033[0m:\033[01;35m%s")' USERNM="if [ \u != ${MYNAME} ]; then whoami; fi;" HOSTNM="if [ \h != ${MYHOST} ]; then echo -n @; hostname; fi;" COLONM="if [ \u != ${MYNAME} ] || [ \h != ${MYHOST} ]; then echo -n :; fi;" PS1="${GREEN}\`${USERNM}\`\`${HOSTNM}\`${WHITE}\`${COLONM}\`${BLUE}\w${GITBRN}\`${DOLLAR}\` "
This code satisfies all my requirements, except that it always leaves a white $ character and does not color it at the corresponding time points . (I suspect that the problem is that "\ $?" In DOLLAR refers to a previously executed command, but DOLLAR is executed last when building PS1, so the previous execute statement is no longer the command that was run before PS1 started, it's something which was done to create the command line.) I am not sure how to solve this problem .
This code is ugly and needs to be reorganized . I tried to move all the color codes into my own variables, but when I used these color variables in the code for GITBRN, things went wrong, so instead I used literal colors instead.
I spent the whole day trying to get this code to work, and I think I won’t go anywhere now. Any suggestions on how to get this dollar sign painted red at the appropriate time would be most appreciated . I am also open to suggestions for refactoring code to make it more understandable and understandable.
PS I am a Ubuntu Linux user (Lucid Lynx).