# .bashrc # 2009-06-17 # bpkroth # # This is a stripped down copy of my current .bashrc file for the purposes of # showing how I use bash, preexec, and screen to display more meaningful # information in ssh sessions. My usual set of rc files is used in several # different environments so a few of the extra bits of error checking and # strange syntax are left in here. And yes all of these comments usually exist # because I expect myself to forget what things are for otherwise. # exit if this is not an interactive shell if [ -z "$PS1" ]; then return fi function bin_in_path { local out="" if ! out=`which $1 2>&1` || echo "$out" | egrep "^no $1 in " > /dev/null; then return 1 else return 0 fi } # Turn the screen session name into something a little bit easier to read. # This will be used by .bash.preexec later. case $TERM in screen*) if [ -z "$LC_MYSTY" ] && [ -n "$STY" ]; then export LC_MYSTY=`echo $STY | awk -F. '( NF == 2 ) { print $2 } ( NF == 3 ) { print $2 FS $3 }'` fi ;; esac # Some tweaks for su/sudo customization export MYUSER=bpkroth if [ $EUID == 0 ]; then export MYCONFDIR=~bpkroth export PATH=/root/bin:$PATH unset DISPLAY umask 0022 else export MYCONFDIR=~bpkroth export USERNAME=$USER umask 0026 # Auto launch screen on SSH connections # Sometimes a pain to deal with SSH screen sessions. # Can be done with C-a a* though. # Also a good idea to always use a hardstatus or caption line in .screenrc if ! [[ $TERM == screen* ]] && bin_in_path screen; then if [ -n "$SSH_TTY" ]; then # This is a remote session. # Connect to the singular screen instance if it is # available, else list if there are multiple, else # start a new one. N=`screen -list | grep -c '[0-9].*tached'` if [ $N == 1 ]; then sleep .5 && screen -X windowlist -b & #exec screen -x -p - screen -x -p - elif [ $N == 0 ]; then #exec screen screen else echo screen -list fi #else # local connection, don't open a screen session by default # screen fi fi fi MYTTY=`tty 2>&1` # Force use of my screenrc file, even if I'm being lazy and "sudo screen". export SCREENRC=$MYCONFDIR/.screenrc # Add a timestamp to history records. export HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S:: ' # Install ZSH style precmd() and preexec() functions. # Useful for setting the title of the terminal to the command that's # running, not just which directory in. This make distinguishing # screen sessions much easier as well. if echo "$MYTTY" | grep '/dev/pts' > /dev/null || echo "$MYTTY" | grep '/dev/tty' > /dev/null; then if [ -f $MYCONFDIR/.bash.preexec ]; then # Note: This requires NOT using DEBUG or PROMPT_COMMAND. It # should also be the last thing we do in the bashrc. unset PROMPT_COMMAND # The script works better if these variables are set. # http://glyf.livejournal.com/63106.html?thread=210818 export SCREEN_HOST=$HOSTNAME # If this is a local connection, also try to set some env # variables that may or may not be transferred to other hosts. # See also: # man sshd_config AcceptEnv # man ssh_config SendEnv if [ -z "$SSH_TTY" ]; then [ -z "$LC_HOST" ] && export LC_HOST=$HOSTNAME [ -z "$LC_USER" ] && export LC_USER=$USER fi if [ $EUID == 0 ]; then export PROMPTCHAR='#' else export PROMPTCHAR='$' fi . $MYCONFDIR/.bash.preexec # The built in functions script pretty much do what I want them to. # I just tweaked the format a little. preexec_xterm_title_install fi fi # Some environment cleanup unset MYTTY