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 6 years ago. Post ID: 2949
Page permalink: http://securitronlinux.com/bejiitaswrath/setting-up-your-bash-prompt-how-to-get-the-most-out-of-the-bash-shell/

Now, we must convince Congress to stop the FCC. Can you display an alert?

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-- \[email protected]\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"'
fi

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. http://www.atomicmpc.com.au
{
	DEFAULT=$(tput sgr0);
	FILELIST=.badlink.list
 
	[ -e $FILELIST ] && $( rm -fr $FILELIST )
 
	function checklink()
	{
		for badlink in $1/*; do
			[ -h "$badlink" -a ! -e "$badlink" ] && echo \
			\"$badlink\" >> $FILELIST
			[ -d "$badlink" ] && checklink $badlink
		done
	}
 
	for directory in `pwd`; do
		if [ -d $directory ] ; then
			checklink $directory;
		fi
	done
 
	if [ -e $FILELIST ] ; then
		for line in $(cat $FILELIST); do
			echo $line | xargs -r rm | echo -e "$line \
			-removed"
			echo
		done
		rm -fr $FILELIST
	else
		printf "Bad symlinks not found.\n\n"
	fi
} # 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}";
	fi
	unset WHOAMI _DISPLAY
fi
 
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