My bashrc file and what it does

My bashrc file and what it does

Even though Linux GUIs have been around forever, much of the administration and configuration is easier in a command line interface (CLI). The default CLI (shell) for most versions of Linux is a powerful tool called bash. To configure bash you need to create and/or edit the .bashrc file in your home directory. This file is read everytime you start a new bash window. I have collected some of the best parts from .bashrc files and listed them below. My complete .bashrc file is listed at the end.

Listing files

The most commonly used command on my system is ls. Here are the ls aliases I use.
# Add color
alias ls='ls -h --color=auto'
# show everything
alias ll='ls -lA'
# DOS typo
alias dir='ll'
# wide listing showing types
alias l='ls -CF'
#sort by size
alias lk='ls -lSr'

Working with directories

# typo fix
alias cd..='cd ..'
# Just up several levels
alias up='cd ..'
alias 2up='cd ../../'
alias 3up='cd ../../../'
alias 4up='cd ../../../../'
# Functions like aliases
# function mcd () { mkdir "$1" ; cd "$1" ; }
function mcd() {
if [ $# != 1 ]; then
echo "Usage: mcd dir"
else
mkdir -p "$1" ; cd "$1"
fi
}
# jump to the old directory
alias back='cd $OLDPWD'
# Show sizes of sub directories. Useful to see what is using disk space
function duck ()
{
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -rf /tmp/list
}

System info

# System info
alias cpuu="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'"
alias memu='ps -e -o rss=,args= | sort -b -k1,1n | pr -TW$COLUMNS'
# grep the ps output
alias psg='ps aux | grep' #requires an argument

Setting the prompt

People preferences with the Linux prompt vary as much as their tastes in music. Below is my simple prompt setting. Also, the PROMPT_COMMAND is run everytime the prompt is displayed. In my case it updates the window title bar with similar information as the prompt. I also define some colors to make things look good.
#######################################################
# Prompt
#######################################################
BGREEN='\033[1;32m'
GREEN='\033[0;32m'
BRED='\033[1;31m'
RED='\033[0;31m'
BBLUE='\033[1;34m'
BLUE='\033[0;34m'
BROWN='\033[0;33m'
YELLOW='\033[1;33m'
BLACK='\033[0;30m'
DARKGRAY='\033[1;30m'
LIGHTGRAY='\033[0;37m'
WHITE='\033[1;37m'
NORMAL='\033[00m'

PS1=”${BLUE}(${NORMAL}\h:${GREEN}\w${BLUE}) ${RED}\$ ${NORMAL}”
# Update the window title
PROMPT_COMMAND=’echo -ne “\033]0;${USER}@${HOSTNAME}: ${PWD}\007″‘

The Welcome screen

I give some system information on the screen when bash is first started.
# WELCOME SCREEN
clear

echo -ne “${BLUE}” “Hello, $USER. today is, “; date
echo -e “${DARKGRAY}”; cal ;
echo -ne “${CYAN}”;
echo -ne “${BLUE}Sysinfo:”;uptime ;echo “”

Important note about DietPi

DietPi includes a bashrc file that has some important functionality. It has one line that calls /DietPi/dietpi/login and it sets up many of the commands the makes DietPi easy to use. You need to make two changes from my default bashrc to work with DietPi. 1) Make sure your bashrc still calls /DietPi/dietpi/login. Just put that at the top of you file. 2) Because the login script has a welcome screen, I removed my default welcome screen. Other than that, everything else will work just as is.

Putting it all together

Here is the complete file. There are some items the I didn’t describe above but they are technical items that, while important, are not that interesting to blog about.
# .bashrc
# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi

#######################################################
# ls Aliases
# -------------
# Add color
alias ls='ls -h --color=auto'
# show everything
alias ll='ls -lA'
# DOS typo
alias dir='ll'
# wide listing showing types
alias l='ls -CF'
#sort by size
alias lk='ls -lSr'

# typo fix
alias cd..='cd ..'
# Just up several levels
alias up='cd ..'
alias 2up='cd ../../'
alias 3up='cd ../../../'
alias 4up='cd ../../../../'
# Functions like aliases
# function mcd () { mkdir "$1" ; cd "$1" ; }
function mcd() {
if [ $# != 1 ]; then
echo "Usage: mcd dir"
else
mkdir -p "$1" ; cd "$1"
fi
}

# add color to grep
alias grep='grep --color=auto'
# jump to the old directory
alias back='cd $OLDPWD'
# Show sizes of sub directories. Useful to see what is using disk space
function duck ()
{
du -shx * .[a-zA-Z0-9_]* 2> /dev/null | \
egrep '^ *[0-9.]*[MG]' | sort -n > /tmp/list
egrep '^ *[0-9.]*M' /tmp/list
egrep '^ *[0-9.]*G' /tmp/list
rm -rf /tmp/list
}

# System info
alias cpuu="ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'"
alias memu='ps -e -o rss=,args= | sort -b -k1,1n | pr -TW$COLUMNS'
# grep the ps output
alias psg='ps aux | grep' #requires an argument

# get network info
function netinfo ()
{
echo "--------------- Network Information ---------------"
/sbin/ifconfig | awk /'inet addr/ {print $2}'
/sbin/ifconfig | awk /'Bcast/ {print $3}'
/sbin/ifconfig | awk /'inet addr/ {print $4}'
/sbin/ifconfig | awk /'HWaddr/ {print $4,$5}'
echo "${myip}"
echo "---------------------------------------------------"
}

#######################################################
# Prompt
#######################################################
BGREEN='\033[1;32m'
GREEN='\033[0;32m'
BRED='\033[1;31m'
RED='\033[0;31m'
BBLUE='\033[1;34m'
BLUE='\033[0;34m'
BROWN='\033[0;33m'
YELLOW='\033[1;33m'
BLACK='\033[0;30m'
DARKGRAY='\033[1;30m'
LIGHTGRAY='\033[0;37m'
WHITE='\033[1;37m'
NORMAL='\033[00m'

PS1="${BLUE}(${NORMAL}\h:${GREEN}\w${BLUE}) ${RED}\$ ${NORMAL}"
# Update the window title
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

#colored manpages
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'

#####################################
# ##### ENVIRONMENT VARIABLES ##### #
#####################################
declare -x HISTFILE=~/.bash_history
declare -x HISTCONTROL=ignoredups
declare -x HISTFILESIZE=100000
declare -x HISTSIZE=100000

# WELCOME SCREEN
clear

echo -ne "${BLUE}" "Hello, $USER. today is, "; date
echo -e "${DARKGRAY}"; cal ;
echo -ne "${CYAN}";
echo -ne "${BLUE}Sysinfo:";uptime ;echo ""

That’s it! Let me know if you have any suggestions or ideas for .bashrc.

Submit a Comment

Your email address will not be published. Required fields are marked *