Shell prompt: \W shown as '\w', left puzzled

0

TL;DR:

PS1 is not as it's supposed to be. PS1="\W" shows as \w█ the block being the blinking cursor. The folder name will appear only after directory change. OS is Debian 9.9 Stretch. I'm not quite sure where I've made a mistake and kindly ask for others to take a look and give their educated opinions.

Below is my .bashrc:

RED='\[\033[31m\]'
BOLD_RED='\[\033[1;31m\]'
YELLOW='\[\033[33m\]'
GREEN='\[\033[32m\]'
BLUE='\[\033[01;34m\]'
BOLD_BLUISH='\[\033[01;32m\]'
NORMAL='\[\033[00m\]'

USR='\u'
TIME='\t'
PWD='\w'
DIR='\W'
RIGHTS='\$'
RESET='\[$(tput sgr0)\]'

if [ $(id -u) -eq 0 ]; then
    PS1="$NORMAL\t·$BOLD_RED\u@\h$NORMAL·$GREEN$DIR·$NORMAL\$ $RESET"
else
    PS1="$GREEN\t$NORMAL·$BOLD_BLUISH\u@\h$NORMAL·$BLUE$DIR$NORMAL·\$ $RESET"
fi

krg

Posted 2019-06-15T18:38:50.737

Reputation: 7

You set PWD to '\w', but PWD is supposed to be the current working directory (the shell sets it every time you change directories). This is why you should use lower- or mixed-case variable names for your stuff; there are a lot of all-caps names with special meanings, and if you accidentally use one of them for something else, it can cause weird problems. Like this. – Gordon Davisson – 2019-06-15T20:07:15.173

Thank you for pointing out the obvious! I'm surprised how I failed to notice it myself. – krg – 2019-06-17T12:02:38.983

Answers

0

It's because PWD is a shell variable. From man 1 bash:

PWD The current working directory as set by the cd command.

Bash updates this variable whenever the current working directory changes. It appears that if PS1 uses \W or \w, the prompt reads from the variable, so if you do (or just did) PWD='\w' then the prompt will show literal \w. Try it with another value (e.g. PWD="Oh boy, this string should not appear in my prompt!") and your prompt will reflect this.

After cd the shell updates the variable and the prompt behaves like PS1="\W" should.

General solution: use lowercase names for your variables.

Kamil Maciorowski

Posted 2019-06-15T18:38:50.737

Reputation: 38 429

Thank you!

Apparently I did make not one, but even two mistakes! First being, the one you pointed out, using commands as variables (dir & pwd) and the second forgetting and mixing up that variables should be lower-, not uppercase. – krg – 2019-06-17T12:02:25.223