A while ago I was playing with bash and zsh because I mainly use zsh as a login shell but on some boxes I have bash (and on some I could not change to zsh because it simply did not exist).
The end result was a .shellrc for both bash and zsh:
.profile:
Code:
# All the functions which are used to setup a correct enviroment.
#
load_env() {
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
WHOAMI=${USER:-$LOGNAME}
PATH=$PATH:$HOME/bin:$HOME/tools
NNTPSERVER=news.euronet.nl
ED=vi
EDITOR=vi
PAGER=less
MAIL=$HOME/mail
TERM=xterm # Some things break when this is screen..
TZ=CET
# With antiword icw a2ps you cannot print pdf files when utf8 is used.
#export LANG=nl_NL.utf8
#LANG=en_US.utf8
export PATH PATH NNTPSERVER ED EDITOR PAGER \
MAIL WHOAMI TERM TZ
CVS_RSH=ssh
export CVS_RSH
}
load_shell() {
running_shell=`ps -p $$ | egrep -v "PID|TTY|TIME|CMD" | awk '{print $NF}' | sed -e 's/-//g' -e 's/(//g' -e 's/)//g' | awk -F\/ '{print $NF}'`
shell_functions="${HOME}/.functions.${running_shell}"
if [ -e ${shell_functions} ] && [ -f ${shell_functions} ]; then
source ${shell_functions}
fi
}
load_alias() {
ALIAS=$HOME/.alias
for alias in $ALIAS*
[email protected]; do
if [ -f $alias ] ; then
source $alias
fi
done
}
load_display() {
case $1 in
'local')
export DISPLAY=:0.0
;;
'*')
unset DISPLAY
;;
esac
}
# MOTD
motd() {
max_user=13
which_users=`w | head -${max_user}`
# Some fun and the date
FORTUNE=/usr/games/fortune
if test ! -f $FORTUNE ; then
FORTUNE=`which fortune`
fi
fortune=""
if [ -x $FORTUNE ] ; then
fortune="
# Fortune of the shell
`$FORTUNE`
"
fi
UNAME=`uname -a`
DATE=`date`
cat <<OEF
# Users logged in (max $max_user)
$which_users
$fortune
$UNAME
OEF
}
functions.bash
Code:
set_term() {
case $TERM in
xterm*|rxvt*)
TITLEBAR='\[\033]0;\u:${NEW_PWD}\007\]'
;;
*)
TITLEBAR=""
;;
esac
}
set_ps1() {
local NONE="\[\033[0m\]" # unsets color to term's fg color
# regular colors
local K="\[\033[0;30m\]" # black
local R="\[\033[0;31m\]" # red
local G="\[\033[0;32m\]" # green
local Y="\[\033[0;33m\]" # yellow
local B="\[\033[0;34m\]" # blue
local M="\[\033[0;35m\]" # magenta
local C="\[\033[0;36m\]" # cyan
local W="\[\033[0;37m\]" # white
# empahsized (bolded) colors
local EMK="\[\033[1;30m\]"
local EMR="\[\033[1;31m\]"
local EMG="\[\033[1;32m\]"
local EMY="\[\033[1;33m\]"
local EMB="\[\033[1;34m\]"
local EMM="\[\033[1;35m\]"
local EMC="\[\033[1;36m\]"
local EMW="\[\033[1;37m\]"
# background colors
local BGK="\[\033[40m\]"
local BGR="\[\033[41m\]"
local BGG="\[\033[42m\]"
local BGY="\[\033[43m\]"
local BGB="\[\033[44m\]"
local BGM="\[\033[45m\]"
local BGC="\[\033[46m\]"
local BGW="\[\033[47m\]"
# without colors: PS1="[\
[email protected]\h \${NEW_PWD}]\\$ "
# extra backslash in front of \$ to make bash colorize the prompt
cur_tty="$(tty)" ; cur_tty="${cur_tty:5}" # The tty we are working on
last_exit="\$?" # Exit code of the command executed from the prompt..
if [ $UID -eq 0 ] ; then # Check for root
UBC=$BGB
UC=$EMR
TERM_STRING='#'
else # Otherwise..
UBC=$BGB
UC=$EMG
TERM_STRING='$'
fi
export PS1="${UBC}${EMW}\t $cur_tty $last_exit${NONE} ${UC}\
[email protected]\h:\w${NONE}${TERM_STRING} "
}
set_shell_options() {
set -o vi
}
functions.zsh
Code:
set_ps1() {
local NONE='%{\e[0m%}' # unsets color to term's fg color
# regular colors
local K='%{\e[0;30m%}' # black
local R='%{\e[0;31m%}' # red
local G='%{\e[0;32m%}' # green
local Y='%{\e[0;33m%}' # yellow
local B='%{\e[0;34m%}' # blue
local M='%{\e[0;35m%}' # magenta
local C='%{\e[0;36m%}' # cyan
local W='%{\e[0;37m%}' # white
# empahsized (bolded) colors
local EMK='%{\e[1;30m%}'
local EMR='%{\e[1;31m%}'
local EMG='%{\e[1;32m%}'
local EMY='%{\e[1;33m%}'
local EMB='%{\e[1;34m%}'
local EMM='%{\e[1;35m%}'
local EMC='%{\e[1;36m%}'
local EMW='%{\e[1;37m%}'
# background colors
local BGK='%{\e[1;40m%}'
local BGR='%{\e[1;41m%}'
local BGG='%{\e[1;42m%}'
local BGY='%{\e[1;43m%}'
local BGB='%{\e[1;44m%}'
local BGM='%{\e[1;45m%}'
local BGC='%{\e[1;46m%}'
local BGW='%{\e[1;47m%}'
if [ $UID -eq 0 ] ; then
UBC=$BGB
UC=$EMR
TERM_STRING='#'
else
UBC=$BGB
UC=$EMG
TERM_STRING='$'
fi
term=$(tty) ; term=`echo $term | sed -e 's/\/dev\///'` ;
export PS1="$(print "${UBC}%T $term %?${NONE} ${UC}%
[email protected]%m:%d${NONE}${TERM_STRING} " )"
}
set_term() {
case $TERM in
xterm*)
precmd () {print -Pn "\e]0;%
[email protected]%m: %~\a"}
;;
esac
}
set_shell_options() {
# zsh specific options
unsetopt promptcr 2>/dev/null # prevent the prompt overwriting output when there is no newline
unsetopt nomatch
unsetopt AUTO_CD
setopt MAIL_WARNING
setopt GLOB_SUBST # global substitution
setopt NO_BEEP # Stay away beep!
setopt CORRECT # correct incorrent cmd's
setopt CORRECTALL # Correct everything
setopt GLOBDOTS # Also look for . files
setopt CSHJUNKIEQUOTES # No stray quotes are allowed
setopt EXTENDEDGLOB # ls ^bla.* will not show ^bla.txt for example
# History
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=$HOME/.zsh_history
# The following options can produce errors
local my_zsh_version=`echo $ZSH_VERSION| cut -d\. -f1`
if test $my_zsh_version -eq 4 ; then
setopt INC_APPEND_HISTORY
setopt SHARE_HISTORY # Share history between session
setopt HIST_EXPIRE_DUPS_FIRST # No duplicates..
setopt HIST_FIND_NO_DUPS # .. no dups ..
fi
# Set vi like editting
bindkey -v
WATCH=notme
}
And finaly bringing it all together:
shellrc
Code:
#
# Bash hack
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
#
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
#if [[ "$-" != *i* ]] ; then
# return
#fi
# We can also do it like this:
[ ! -t 0 ] && return
source $HOME/.profile
load_env
load_alias
load_shell
set_term
set_ps1
set_shell_options
motd
# Load some extras
source $HOME/.env.local
The .env.local is there to override any alias/function whatever because on the box something is different from the rest.
There is a small TODO:
* Expand bash shell options to reflect the zsh shell options (but this is unlikely to happen since I am a zsh user and I don't really use the bash shell that much).