Very comprehensive .bashrc file for your Linux system.

Posted: May 2, 2017. At: 8:55 PM. This was 4 months ago. Post ID: 10605

Complex .bashrc file for Linux or UNIX

Very good and very complex .bashrc file for a Linux or UNIX system. I used this one ages ago and it is great to use. I have tried this on FreeBSD and Open Solaris and it works on either.

#!/bin/bash
# mode:shell-script
# sh-shell:bash
# Modified to work on Open Solaris. Much better than FreeBSD.
# There are still problems with ls but I am working on that.
# Bejiitas Wrath ~/.bashrc.
[ -f /etc/bashrc ] && . /etc/bashrc
[ -f /etc/profile ] && . /etc/profile
 
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
 
OSRELEASE=`uname -ir`;
LD_LIBRARY_DIR=$LD_LIBRARY_DIR:/home/gandalf/opt/lib
PATH=$PATH:/home/gandalf/opt/bin
NUM=`mynum`;
 
# Author: John Cartwright.
# Website: http://members.tripod.com/bejiitas_wrath/
# I am including many useful if statements in this
# .bashrc so that if a needed program is not installed
# it will still give some alternate output.
 
# 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
 
if [ "$PS1" ]; then
	export HISTCONTROL=ignoredups
	export EDITOR=vim
#       Which one suits you?
	export WINDOWMANAGER=gnome-session
	export DOOMWADDIR="/usr/local/games" # For doom!
	# These two examples from bash-doc.
	export PAGER="/usr/bin/less"
	export LESS="-i -e -M -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-..."
	shopt -s checkwinsize
	alias dir='ls --format="vertical"'
	alias vdir='ls --format="long"'
	alias me='who am i'
	alias verbosetree='tree -A -s -p -f --dirsfirst'
	alias psverbose='ps u a x f g'
	alias ll='ls -l | sort -nr'
	alias la='ls -A'
	alias l='ls -CF'
	alias dsize="du -ack | sort -nr | head -n 20"
	alias psview="pstree -a -u -G -l -h -p"
 
# These following functions from http://www.die.net/doc/linux/abs-guide/sample-bashrc.html
function my_ip() # get IP adresses. Bracket on next line C style...
{
    MY_IP=$(/sbin/ifconfig ppp0 | awk '/inet/ { print $2 } ' | sed -e s/addr://)
    MY_ISP=$(/sbin/ifconfig ppp0 | awk '/P-t-P/ { print $3 } ' | sed -e s/P-t-P://)
}
 
function lowercase()  # move filenames to lowercase.
{
    for file ; do
        filename=${file##*/}
        case "$filename" in
        */*) dirname==${file%/*} ;;
        *) dirname=.;;
        esac
        nf=$(echo $filename | tr A-Z a-z)
        newname="${dirname}/${nf}"
        if [ "$nf" != "$filename" ]; then
            mv "$file" "$newname"
            echo "lowercase: $file --> $newname"
        else
            echo "lowercase: $file not changed."
        fi
    done
}
 
function badlink() # From Atomic magazine #43 August 2004.
{
	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"
		done
		rm -fr $FILELIST
	else
		printf "Bad symlinks not found."
	fi
} # End atomic function.
 
# Setting the value of the Xterm title.
# Only if we are in X...
if [ $DISPLAY ] ; then
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${OSRELEASE}: ${PWD}\007"'
fi
# Done...
 
case $TERM in # Testing for either xterm or rxvt or uxvt or uxterm...
		# etc. Works in a xterm on Open Solaris just fine..
*term | *xvt )
PS1='\n[ \e[041m\e[40m\e[37m\e[1m\u@${OSRELEASE}\e[41m\e[0m ]\n[ \e[033mJobs \j.\e[0m \
\e[30m\e[41m\e[1m\e[33mPWD: \w. \s \V.\e[0m ] [\e[4m \#-:\$ \e[0m]\n-> '
	;;
esac
fi
 
# Outside of X we use this!
if [ "$TERM" == "linux" ]; then
        PS1='\e[31m\e[40m\e[37m\e[1m\u@${OSRELEASE}.\e[0m\n \e[30m\e[41m\e[1m\e[33mPWD: \
		\w.\e[0m \s.\n \V. \# \$> '
fi
 
# Outside of xinit we use this! For Open Solaris. Which rules over FreeBSD Btw.
if [ "$TERM" == "sun-color" ]; then
        PS1='\u@${OSRELEASE}.\n PWD: \w. \s.\n \V. \# \$> '
fi
 
# The following code from http://www.die.net/doc/linux/abs-guide/sample-bashrc.html
 
if [ "${BASH_VERSION%.*}" \< "2.05" ]; then
    echo "You will need to upgrade to version 2.05 for programmable completion"
    return
fi
 
shopt -s extglob        # necessary
set +o nounset	    # otherwise some completions will fail
 
complete -A hostname   rsh rcp telnet rlogin r ftp ping disk
complete -A command    nohup exec eval trace gdb
complete -A command    command type which
complete -A export     printenv
complete -A variable   export local readonly unset
complete -A enabled    builtin
complete -A alias      alias unalias
complete -A function   function
complete -A user       su mail finger
 
complete -A helptopic  help     # currently same as builtins
complete -A shopt      shopt
complete -A stopped -P '%' bg
complete -A job -P '%'     fg jobs disown
 
complete -A directory  mkdir rmdir
complete -A directory   -o default cd
 
complete -f -d -X '*.gz'  gzip
complete -f -d -X '*.bz2' bzip2
complete -f -o default -X '!*.gz'  gunzip
complete -f -o default -X '!*.bz2' bunzip2
complete -f -o default -X '!*.pl'  perl perl5
complete -f -o default -X '!*.ps'  gs ghostview ps2pdf ps2ascii
complete -f -o default -X '!*.dvi' dvips dvipdf xdvi dviselect dvitype
complete -f -o default -X '!*.pdf' acroread pdf2ps
complete -f -o default -X '!*.+(pdf|ps)' gv
complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf
complete -f -o default -X '!*.tex' tex latex slitex
complete -f -o default -X '!*.lyx' lyx
complete -f -o default -X '!*.+(jpg|gif|xpm|png|bmp)' xv gimp
complete -f -o default -X '!*.mp3' mpg123
complete -f -o default -X '!*.ogg' ogg123
 
#=====================================================================================#
# This is a 'universal' completion function - it works when commands have
# a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a'
_universal_func ()
{
    case "$2" in
	-*)	;;
	*)	return ;;
    esac
 
    case "$1" in
	\~*)	eval cmd=$1 ;;
	*)	cmd="$1" ;;
    esac
    COMPREPLY=( $("$cmd" --help | sed  -e '/--/!d' -e 's/.*--\([^ ]*\).*/--\1/'| \
grep ^"$2" |sort -u) )
}
complete  -o default -F _universal_func ldd wget bash id info
 
 
_make_targets ()
{
    local mdef makef gcmd cur prev i
 
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
 
    # if prev argument is -f, return possible filename completions.
    # we could be a little smarter here and return matches against
    # `makefile Makefile *.mk', whatever exists
    case "$prev" in
        -*f)    COMPREPLY=( $(compgen -f $cur ) ); return 0;;
    esac
 
    # if we want an option, return the possible posix options
    case "$cur" in
        -)      COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
    esac
 
    # make reads `makefile' before `Makefile'
    if [ -f makefile ]; then
        mdef=makefile
    elif [ -f Makefile ]; then
        mdef=Makefile
    else
        mdef=*.mk               # local convention
    fi
 
    # before we scan for targets, see if a makefile name was specified
    # with -f
    for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
        if [[ ${COMP_WORDS[i]} == -*f ]]; then
            eval makef=${COMP_WORDS[i+1]}       # eval for tilde expansion
            break
        fi
    done
 
        [ -z "$makef" ] && makef=$mdef
 
    # if we have a partial word to complete, restrict completions to
    # matches of that word
    if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi
 
    # if we don't want to use *.mk, we can take out the cat and use
    # test -f $makef and input redirection
    COMPREPLY=( $(cat $makef 2>/dev/null | awk 'BEGIN {FS=":"} /^[^.#   ][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
}
 
complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake
 
_configure_func ()
{
    case "$2" in
        -*)     ;;
        *)      return ;;
    esac
 
    case "$1" in
        \~*)    eval cmd=$1 ;;
        *)      cmd="$1" ;;
    esac
 
    COMPREPLY=( $("$cmd" --help | awk '{if ($1 ~ /--.*/) print $1}' | grep ^"$2" | sort -u) )
}
 
complete -F _configure_func configure
 
# cvs(1) completion
_cvs ()
{
    local cur prev
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}
 
    if [ $COMP_CWORD -eq 1 ] || [ "${prev:0:1}" = "-" ]; then
	COMPREPLY=( $( compgen -W 'add admin checkout commit diff \
	export history import log rdiff release remove rtag status \
	tag update' $cur ))
    else
	COMPREPLY=( $( compgen -f $cur ))
    fi
    return 0
}
complete -F _cvs cvs
 
_killall ()
{
    local cur prev
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
 
    # get a list of processes (the first sed evaluation
    # takes care of swapped out processes, the second
    # takes care of getting the basename of the process)
    COMPREPLY=( $( /usr/bin/ps -u $USER -o comm  | \
        sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
        awk '{if ($0 ~ /^'$cur'/) print $0}' ))
 
    return 0
}
 
complete -F _killall killall killps
 
# End: Bejiitas Wrath .bashrc.

Very complex .xinitrc file for Linux. This is a very good way to start a window manager when the user is not using GDM or KDM.

#!/bin/bash
 
# Sample .xinitrc for SuSE Linux
# This script is called from 'startx' when you start an X session
 
# In case everything goes wrong, we at least fall back to a plain xterm
#
if [ $WINDOWMANAGER="failsafe" ] ; then
    failsafe="xterm -ls -T Failsafe -geometry 80x24-0-0"
    trap "exec $failsafe" EXIT SIGHUP SIGINT SIGPIPE SIGTERM SIGIO
fi
 
# Some bash (1 and 2) settings to avoid trouble on a
# failed program call.
 
set +e > /dev/null 2>&1
set +u > /dev/null 2>&1
set +o posix  > /dev/null 2>&1
if type shopt > /dev/null 2>&1 ; then
    shopt -s execfail
else
    no_exit_on_failed_exec=1
fi
 
# Run user xinit scripts.
#
for script in /etc/X11/xinit/xinitrc.d/*; do
    test -d $script && continue
    test -x $script || continue
    case "$script" in
	.*)		continue ;;
	*.rpm*)		continue ;;
	*.swap)		continue ;;
	*.bak)		continue ;;
	*.orig)		continue ;;
	\#*)		continue ;;
	*~)		continue ;;
    esac
    $script
done
unset script
 
#
# Source common code shared between the
# X session and X init scripts
#
. /etc/X11/xinit/xinitrc.common
 
 
case $WINDOWMANAGER in
    fvwm-themes-start)
	wmsetbg ~/bg.jpg
	;;
esac
 
if [!$WINDOWMANAGER] ; then
    xsetroot -solid darkblue
fi
 
# Uncomment next line to activate asking for ssh passphrase
 
ssh-add
 
# finally start the window manager
#
unset WINDOW_MANAGER STARTUP
exec $WINDOWMANAGER
 
exit 0

My .bash_aliases file for Linux.

# My .bash_aliases file. You need this to go with the
# .bashrc below.

alias cls='clear'
alias lu='ls -Fula'
alias tarunpack='tar -zxvf'
alias bz2unpack='tar -jxvf'
alias me='who am i'
alias verbosetree='tree -A -s -p -f --dirsfirst'
alias psverbose='ps u a x f g'
alias dsize="du -ack | sort -nr | head -n 20"
alias psview="pstree -a -u -G -l -h -p"
alias dir='ls -Fula'

The .bash_logout file. This is executed when you logout of a session. On a virtual terminal, this will clear the screen.

# ~/.bash_logout: executed by bash(1) when login shell exits.
 
# when leaving the console clear the screen to increase privacy
 
if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

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

Leave a Reply