Tweaking the bash prompt.
In certain cases, the bash prompt, usually of the form username@hostname:workingdir$ , becomes too large for the terminal width. Commands get wrapped around and it doesn't look aesthetically pleasing.
So, i set about trying to learn how the bash prompt can be modified and if there's a way to dynamically change it depending on the terminal width.
$ echo $COLUMNS
# print the current terminal width
$ expr length username@hostname:$(pwd)
# prints the current length of the bash prompt,
# which would vary depending on the working dir.
# now, i want to change the prompt when it is half the width
$ expr `expr length rahulporuri@astronut:$(pwd)` "*" "2"
# can be used to double the length of the prompt and
$ if ["var1" -gt "var2"]; then do ... ; fi
# to compare the two variables - length and width
# So far, I've only been able to set a static change i.e
# if the length of the prompt is over 40 chars in length,
# then the prompt will not display the working dir!
$ if [ "$(expr length rahulporuri@astronut:$(pwd))" -gt 40 ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
fi
# PS1 is the bash prompt,
# where \u is for the username,
# \h is for the host name and
# \w if for the working dir.
# [\...\] refer to the color codes.