Fun with Bash

From Mage
Jump to: navigation, search

Random bits of fun available when using the bash shell.

Custom Shell Appearance

...because if I see

root# logou^?^?^?^C

one more time I'm going to kill sh's firstbourne. Plus, doesn't this seem a tad more readable and friendly?


Note that I didn't have to tell it to reload bash after becoming root. It shows more information than the default loadout so it could benefit anyone who has to deal with copy/pasted output. How does one do this, one might ask?

How Indeed

Within your home directory are two files we'll create called .bashrc and .bash_login. .bash_login is the file that bash attempts to load when you use the shell as a login shell (ie: this is what PuTTY is going to get you when you login). .bashrc is the profile that is loaded when you don't call it as a login shell (ie: you just su -'d and lolrootusesSH).

vi ~/.bashrc

umask 0022

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# We can indeed, control history!
shopt -s histappend

# Oh my glob, let me do globular expressions!
shopt -s extglob

# Try to force color

# If this is an xterm set the title to user@host:dir
if [[ ${EUID} == 0 ]] ; then

# Don't put duplicate lines in the history
export HISTCONTROL=ignoredups

# Set your preferred editor
export EDITOR="vi"

# Check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

##### Set a double-lined, color shell appearance #####
PS1="\n\[\033[1;37m\]\342\224\214($(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\u@\h'; else echo '\[\033[01;34m\]\u@\h'; fi)\[\033[1;37m\])\$([[ \$? != 0 ]] && echo \"\342\224\200(\[\033[0;31m\]\342\234\227\[\033[1;37m\])\")\342\224\200(\[\033[1;34m\]\@ \d\[\033[1;37m\])\[\033[1;37m\]\342\224\200(\[\033[1;32m\]\w\[\033[1;37m\])\n\342\224\224> \[\033[0m\]"

##### ALIASES #####
alias reload='source ~/.bashrc'
alias back='cd $OLDPWD'
alias ..="cd .."
alias ll="ls -lh"
alias la="ls -lah"
alias lz="ls -laZ"
alias myip="nslookup $HOSTNAME"

# ssu will try its best to get you to a colorful root prompt without a password
alias ssu="sudo su - root -c '`which bash` --rcfile /home/foo/.bashrc'"

# Identify information about your CPU
alias linSocket='cat /proc/cpuinfo | grep "physical id" | sort -u | wc -l'
alias linCore='egrep -e "core id" -e ^physical /proc/cpuinfo|xargs -l2 echo|sort -u | grep "0 core" | wc -l'
alias linThread='egrep -e "core id" -e ^physical /proc/cpuinfo|xargs -l2 echo|sort -u | wc -l'
alias linModel='cat /proc/cpuinfo | grep "model name" | head -1 | cut -d":" -f2 | tr -s " "'
alias linCPU='echo "Model: `linModel`" && echo "Sockets: `linSocket`" && echo "Cores per socket: `linCore`" && echo "Total threads: `linThread`"'

# Handy iptables shortcuts
alias sri="systemctl restart iptables"
alias vip="vi /etc/iptables/iptables.rules"
alias ipn="iptables -nL"

Save that profile as .bashrc and ln -s ~/.bashrc ~/.bash_login so it's loaded in either circumstance. Obviously change the username from foo to your own. Feel free to make any changes or add as many aliases as your heart desires. It's your profile. Use it how you need it.

But PuTTY Can't Read UTF?!

Yes it can. You just don't have it set to. ISO is a shoddy way of going about charactersets anyways, so just save your default as:


Bash Scripting-fu

Bash Traps

Bash provides a "pseudo-signal" of sorts, called EXIT, that can be trapped and used to execute commands or functions when a script exists for ANY reason.

function im_done
     put stuff here
trap im_done EXIT

That's all there is to the basic structure/syntax of using traps. A common use for it is to cleanup temporary files or directories you might be using throughout your script, no matter what happens during it (i.e., it bombs horrifically).

scratch=$(mktemp -d -t tmp.XXXX)
function im_done
     rm -rf "$scratch"
trap im_done EXIT

###here we do something...move something..., whatever we're doing that requires a temp directory
###I know, I know...converting mp3 to ogg, it's contrived. Who cares.

curl -q "" -o "$scratch/foo.mp3"
convert_to_ogg "$scratch/foo.mp3" foo.ogg

#And now when the script is done, our "im_done" function will be called, cleaning up our scratch directory for us. 
#Even if the script exits abnormally, the trapped function will ALWAYS execute (barring of course something 
#catastrophic happening to the shell itself).

This seems like a small amount of information...

That's because it is. These aren't even the tip of the iceberg when it comes to tips and tricks in bash.

If you aren't sated, there's always Arch's Bash Page or our Linux section.