Bash Profile Setting not working for path - PS1
Pregunta
I've written the following bash profile settings file:
BLACK="\[\033[0;30m\]"
DARK_GRAY="\[\033[1;30m\]"
LIGHT_GRAY="\[\033[0;37m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_CYAN="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
LIGHT_RED="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
DEFAULT_COLOR="\[\033[00m\]"
function prompt_command {
# How many characters of the $PWD should be kept
local pwd_length=23
if [ $(echo -n $PWD | wc -c | tr -d " ") -gt $pwd_length ]
then
newPWD="$(echo -n $PWD | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
else
newPWD="$(echo -n $PWD)"
fi
}
PROMPT_COMMAND=prompt_command;
PS1="\`if [ \$? = 0 ];
then
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $newPWD $GREEN$ $DEFAULT_COLOR';
else
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $newPWD $RED$ $DEFAULT_COLOR';
fi; \`"
export PS1;
For some reason getting the newPWD is not working. It is present in the enviroment (I can print it with the echo), however it isn't added to the command lines prompt. There an empty string is added always. So a sample output is:
[11:54:09] ber@szak: $
Any ideas why not?
PS. Here it is my solution to this:
BLACK="\[\033[0;30m\]"
DARK_GRAY="\[\033[1;30m\]"
LIGHT_GRAY="\[\033[0;37m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
CYAN="\[\033[0;36m\]"
LIGHT_CYAN="\[\033[1;36m\]"
RED="\[\033[0;31m\]"
LIGHT_RED="\[\033[1;31m\]"
PURPLE="\[\033[0;35m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
BROWN="\[\033[0;33m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
DEFAULT_COLOR="\[\033[00m\]"
export MYPATHPS='$(
echo -n "${PWD/#$HOME/~}" |
awk -F "/" '"'"'{
if (length() > 14) {
if (NF>4) print "/" "/.../" $(NF-1) "/" $NF;
else if (NF>3) print "/" "/.../" $NF;
else print "/.../" $NF;
}
else print ;
}'"'"'
)';
PS1="$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h \` if [ \$? = 0 ]
then
echo -en '$GREEN'
else
echo -en '$RED'
fi;
\` $MYPATHPS $ $DEFAULT_COLOR ";
export PS1;
Solución
You need to export
newPWD. Just because you can echo
it doesn't mean its in the environment. It means its set in the shell and/or the environment. To know if something is in the environment I would use:
env | grep '^newPWD'
I'm not sure but I don't think that using newPWD
like this is going to work, as newPWD
is not reset every time you change directory like PWD
is. So you may end up needing to change your PS1
setting to call the prompt_command
function each time.
From your comments looks like my guess about newPWD
was right. So what you need to do is change the prompt_command
function to echo
the prompt that you want. Then change your PS1
setting to
PS1="\`if [ \$? = 0 ];
then
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $(prompt_command) $GREEN $DEFAULT_COLOR';
else
echo -e '$PURPLE[\t] $LIGHT_CYAN\u$YELLOW@\h$WHITE: $(prompt_command) $RED $DEFAULT_COLOR';
fi; \`"
export PS1;
Note: I also removed the trailing $
from $RED$
and $GREEN$
as I assumed they were typos. Not sure if that was right