Setting up your BASH prompt. How to get the most out of the BASH shell.

Posted: March 18, 2012. At: 3:59 PM. This was 5 years ago. Post ID: 2949

Setting up your BASH prompt.

A nice BASH shell prompt. This shows the current time and the current folder you are in as well as a full directory path if you are in a subfolder.

PS1="-\t-- \u@\h [\w]\$ "

How to set the contents of the xterm title bar. Using the PROMPT_COMMAND variable. This displays the current directory path in the titlebar. Very useful indeed.

# Setting the value of the Xterm title.
# Only if we are in X...
if [ $DISPLAY ] ; then
	PROMPT_COMMAND='echo -ne "\033]0;${OSRELEASE}: ${PWD}\007"'

This function will scan a folder and find any symbolic links that do not point anywhere. Very useful indeed.

function badlink()
# From Atomic magazine #43 August 2004.
	DEFAULT=$(tput sgr0);
	[ -e $FILELIST ] && $( rm -fr $FILELIST )
	function checklink()
		for badlink in $1/*; do
			[ -h "$badlink" -a ! -e "$badlink" ] && echo \
			\"$badlink\" >> $FILELIST
			[ -d "$badlink" ] && checklink $badlink
	for directory in `pwd`; do
		if [ -d $directory ] ; then
			checklink $directory;
	if [ -e $FILELIST ] ; then
		for line in $(cat $FILELIST); do
			echo $line | xargs -r rm | echo -e "$line \
		rm -fr $FILELIST
		printf "Bad symlinks not found.\n\n"
} # End Atomic function.

How to find out what your IP address is. Using ifconfig and awk.

sudo /sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' | sed -e s/addr://

More useful variables to have in your ~/.bashrc.

# Setting a blinking block cursor for the console.
echo -e '\033[?6c'
# try to set DISPLAY smart (from Hans) :)
# From a SUSE .bashrc file.
if test -z "$DISPLAY" -a "$TERM" = "xterm" -a -x /usr/bin/who ; then
	WHOAMI="`/usr/bin/who am i`"
	_DISPLAY="`expr "$WHOAMI" : '.*(\([^\.][^\.]*\).*)'`:0.0"
	if [ "${_DISPLAY}" != ":0:0.0" -a "${_DISPLAY}" != " :0.0" \
		-a "${_DISPLAY}" != ":0.0" ]; then
		export DISPLAY="${_DISPLAY}";
export EDITOR=mcedit
export TIME_STYLE=+" %d-%m-%y %I:%M %P"
# These two examples from bash-doc.
export PAGER="/usr/bin/less"
export VIEWER="mcedit"
export LESS="-i -e -M -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-..."
shopt -s checkwinsize
[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "`dircolors -b $DIR_COLORS`"

No comments have been made. Use this form to start the conversation :)

Leave a Reply