Bash Shell - Tricks & Tips

Bash Configuration Files

Bash defines three configurations files:

  1. .bash_profile - environmental variables
  2. .bashrc - aliases, shell options
  3. .bash_logout - executed on logout

Enhance your bash prompt

Create /etc/profile.d/system.sh

#!/bin/sh
# 36=lt-blue, 35=purple, 34=dk-blue, 32=green, 31=red

if [ "$SHELL" = "/bin/ash" ]; then
PS1='$ '
else
PS1='\[\033[1;36m\]\u@\h:\w\$\[\033[0m\] '
fi
PS2='> '
export PS1 PS2
alias lss='ls -Fla --color=always'
alias cp='cp -i'

Update your clock

There's lots of fluff involved with updating your clock; all you really need is to keep it simple, despite ntpdate's somewhat eccentric documentation. Add this to your crontab:

0 0 * * * /usr/sbin/ntpdate -us -t 3 tick.ucla.edu usno.pa-x.dec.com bigben.cac.washington.edu

Add this to your system startup file. Note the extra "b" flag, which does an immediate update of the system clock rather than a gradual update.

/usr/sbin/ntpdate -ubs -t 3 tick.ucla.edu usno.pa-x.dec.com bigben.cac.washington.edu
/sbin/hwclock --systohc

Bash shell auto logout

To make the bash shell automatically logout after not being used for a period of time, set the variable TMOUT to the time in seconds of no input before logout. This can be particularly useful in improving security if people forget to logout or leave their terminals unattended.

$ export TMOUT=1800

This will auto logout after 1800 seconds (30 minutes).

Converting newlines

This bit comes from http://www.student.northpark.edu/pemente/sed/sed1line.txt.

# Convert DOS newlines (CR/LF) to Unix format
sed 's/.$//' # assumes that all lines end with CR/LF

# Convert Unix newlines (LF) to DOS format
sed 's/$'"/`echo \\\r`/" # command line under bash