]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .bashrc
.bashrc: short-circuit for non-interactive sessions
[dotfiles.git] / .bashrc
1 #!/bin/bash
2 # ~/.bashrc: executed by bash(1) for non-login shells.
3 # A basically sane bash environment.
4 # Tony Duckles <http://nynim.org/about/> (based on http://github.com/rtomayko/dotfiles)
5
6 # short-circuit for non-interactive sessions
7 [ -z "$PS1" ] && return
8
9 # the basics
10 : ${HOME=~}
11 : ${LOGNAME=$(id -un)}
12 : ${UNAME=$(uname)}
13 # strip OS type and version under Cygwin (e.g. CYGWIN_NT-5.1 => Cygwin)
14 UNAME=${UNAME/CYGWIN_*/Cygwin}
15
16 # complete hostnames from this file
17 HOSTFILE=~/.ssh/known_hosts
18
19 # readline config
20 INPUTRC=~/.inputrc
21
22 # if current $TERM isn't valid, fall-back to TERM=xterm-color or TERM=xterm
23 case $(tput colors 2>&1) in
24 tput* )
25 export TERM=xterm-color
26 case $(tput colors 2>&1) in
27 tput* )
28 export TERM=xterm
29 ;;
30 esac
31 ;;
32 esac
33
34 if [ "$UNAME" = "Darwin" ]; then
35 # Fink init, for OSX
36 test -r /sw/bin/init.sh &&
37 source /sw/bin/init.sh
38 fi
39
40 # ----------------------------------------------------------------------
41 # SHELL OPTIONS
42 # ----------------------------------------------------------------------
43
44 # bring in system bashrc
45 test -r /etc/bashrc &&
46 . /etc/bashrc
47
48 # notify of bg job completion immediately
49 set -o notify
50
51 # shell opts. see bash(1) for details
52 shopt -s cdspell >/dev/null 2>&1
53 shopt -s extglob >/dev/null 2>&1
54 shopt -s histappend >/dev/null 2>&1
55 shopt -s hostcomplete >/dev/null 2>&1
56 shopt -s interactive_comments >/dev/null 2>&1
57 shopt -u mailwarn >/dev/null 2>&1
58 shopt -s no_empty_cmd_completion >/dev/null 2>&1
59
60 # don't check for new mail
61 unset MAILCHECK
62
63 # disable core dumps
64 ulimit -S -c 0
65
66 # default umask
67 umask 0022
68
69 # ----------------------------------------------------------------------
70 # PATH
71 # ----------------------------------------------------------------------
72
73 # we want the various sbins on the path along with /usr/local/bin
74 PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"
75 PATH="/usr/local/bin:$PATH"
76
77 # put ~/bin on PATH if you have it
78 test -d "$HOME/bin" &&
79 PATH="$HOME/bin:$PATH"
80
81 # ----------------------------------------------------------------------
82 # ENVIRONMENT CONFIGURATION
83 # ----------------------------------------------------------------------
84
85 # detect interactive shell
86 case "$-" in
87 *i*) INTERACTIVE=yes ;;
88 *) unset INTERACTIVE ;;
89 esac
90
91 # detect login shell
92 case "$0" in
93 -*) LOGIN=yes ;;
94 *) unset LOGIN ;;
95 esac
96
97 # enable en_US locale w/ utf-8 encodings if not already
98 # configured
99 : ${LANG:="en_US.UTF-8"}
100 : ${LANGUAGE:="en"}
101 : ${LC_CTYPE:="en_US.UTF-8"}
102 : ${LC_ALL:="en_US.UTF-8"}
103 export LANG LANGUAGE LC_CTYPE LC_ALL
104
105 # ignore backups, CVS directories, python bytecode, vim swap files
106 FIGNORE="~:CVS:#:.pyc:.swp:.swa:apache-solr-*"
107
108 # history stuff
109 HISTCONTROL=ignoreboth
110 HISTFILESIZE=10000
111 HISTSIZE=10000
112
113 # ----------------------------------------------------------------------
114 # PAGER / EDITOR
115 # ----------------------------------------------------------------------
116
117 # See what we have to work with ...
118 HAVE_VIM=$(command -v vim)
119
120 # EDITOR
121 test -n "$HAVE_VIM" &&
122 EDITOR=vim ||
123 EDITOR=vi
124 export EDITOR
125
126 # PAGER
127 if test -n "$(command -v less)" ; then
128 PAGER="less -Fir"
129 MANPAGER="less -FiRs"
130 else
131 PAGER=more
132 MANPAGER="$PAGER"
133 fi
134 export PAGER MANPAGER
135
136 # Ack
137 ACK_PAGER="$PAGER"
138 ACK_PAGER_COLOR="$PAGER"
139
140 # ----------------------------------------------------------------------
141 # PROMPT
142 # ----------------------------------------------------------------------
143
144 # http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
145
146 if [ "$UID" = 0 ]; then
147 # root
148 PS_C1="\[\033[1;31m\]" # red
149 PS_C2="\[\033[0;37m\]" # grey
150 PS_P="#"
151 else
152 case "$UNAME" in
153 Cygwin)
154 PS_C1="\[\033[0;32m\]" # green
155 PS_C2="\[\033[0;37m\]" # grey
156 ;;
157 Darwin)
158 PS_C1="\[\033[1;97m\]" # white
159 PS_C2="\[\033[0;37m\]" # grey
160 ;;
161 SunOS)
162 PS_C1="\[\033[1;96m\]" # cyan
163 PS_C2="\[\033[0;36m\]" # cyan
164 ;;
165 *)
166 PS_C1="\[\033[1;93m\]" # yellow
167 PS_C2="\[\033[0;33m\]" # brown
168 esac
169 PS_P="\$"
170 fi
171
172 prompt_simple() {
173 unset PROMPT_COMMAND
174 PS1="[\u@\h:\w]\$ "
175 PS2="> "
176 }
177
178 prompt_compact() {
179 unset PROMPT_COMMAND
180 PS1="${PS_C1}${PS_P}\[\033[0m\] "
181 PS2="> "
182 }
183
184 prompt_color() {
185 PS1="\[\033[0;90m\][${PS_C1}\u@\h\[\033[0m\]\[\033[0;90m\]:${PS_C2}\w\[\033[0;90m\]]${PS_P}\[\033[0m\] "
186 PS2="> "
187 }
188
189 # ----------------------------------------------------------------------
190 # MACOS X / DARWIN SPECIFIC
191 # ----------------------------------------------------------------------
192
193 if [ "$UNAME" = Darwin ]; then
194 # put ports on the paths if /opt/local exists
195 test -x /opt/local -a ! -L /opt/local && {
196 PORTS=/opt/local
197
198 # setup the PATH and MANPATH
199 PATH="$PORTS/bin:$PORTS/sbin:$PATH"
200 MANPATH="$PORTS/share/man:$MANPATH"
201
202 # nice little port alias
203 alias port="sudo nice -n +18 $PORTS/bin/port"
204 }
205 fi
206
207 # ----------------------------------------------------------------------
208 # ALIASES / FUNCTIONS
209 # ----------------------------------------------------------------------
210
211 # alias 'vi' to 'vim' if Vim is installed
212 vim="$(type -P vim)"
213 test -n "$vim" && {
214 alias vi='vim'
215 }
216 unset vim
217
218 # disk usage with human sizes and minimal depth
219 alias du1='du -h --max-depth=1'
220 alias fn='find . -name'
221 alias hi='history | tail -20'
222
223 # For Solaris, use GNU versions of grep and sed
224 if [ "$UNAME" = SunOS ]; then
225 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep"
226 test -x /usr/gnu/bin/sed && alias sed="/usr/gnu/bin/sed"
227 fi
228
229 # Alias csh-style "rebash" to bash equivalent
230 alias rehash="hash -r"
231
232 # ----------------------------------------------------------------------
233 # BASH COMPLETION
234 # ----------------------------------------------------------------------
235
236 test -z "$BASH_COMPLETION" && {
237 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
238 test -n "$PS1" && test $bmajor -gt 1 && {
239 # search for a bash_completion file to source
240 for f in /usr/local/etc/bash_completion \
241 /usr/pkg/etc/bash_completion \
242 /opt/local/etc/bash_completion \
243 /etc/bash_completion
244 do
245 test -f $f && {
246 . $f
247 break
248 }
249 done
250 }
251 unset bash bmajor bminor
252 }
253
254 # override and disable tilde expansion
255 _expand() {
256 return 0
257 }
258
259 # ----------------------------------------------------------------------
260 # LS AND DIRCOLORS
261 # ----------------------------------------------------------------------
262
263 # we always pass these to ls(1)
264 unset LS_COMMON
265 ## OS-specific options
266 #case "$UNAME" in
267 # "Linux" ) LS_COMMON="--color=auto";;
268 # "Cygwin" ) LS_COMMON="--color=auto";;
269 # "Darwin" ) LS_COMMON="--color=auto";;
270 #esac
271
272 # if the dircolors utility is available, set that up for ls
273 dircolors="$(type -P gdircolors dircolors | head -1)"
274 test -n "$dircolors" && {
275 COLORS=/etc/DIR_COLORS
276 test -e "/etc/DIR_COLORS.$TERM" && COLORS="/etc/DIR_COLORS.$TERM"
277 test -e "$HOME/.dircolors" && COLORS="$HOME/.dircolors"
278 test ! -e "$COLORS" && COLORS=
279 eval `$dircolors --sh $COLORS`
280 }
281 unset dircolors
282
283 # enable color ls output if available
284 test -n "$COLORS" &&
285 LS_COMMON="--color=auto $LS_COMMON"
286
287 # setup the main ls alias if we've established common args
288 test -n "$LS_COMMON" &&
289 alias ls="command ls $LS_COMMON"
290
291 # these use the ls aliases above
292 alias ll="ls -l"
293 alias l.="ls -d .*"
294 alias ll.="ls -ld .*"
295 alias lla="ls -la"
296
297 # setup color grep output if available
298 if [ -n "$COLORS" ]; then
299 case "$UNAME" in
300 "SunOS")
301 # For Solaris, use the GNU version of grep for color support
302 test -x /usr/gnu/bin/grep && alias grep="/usr/gnu/bin/grep --color=auto"
303 ;;
304 *)
305 alias grep="command grep --color=auto"
306 ;;
307 esac
308 fi
309
310 # --------------------------------------------------------------------
311 # MISC COMMANDS
312 # --------------------------------------------------------------------
313
314 # push SSH public key to another box
315 push_ssh_cert() {
316 local _host
317 test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -b 4096
318 for _host in "$@";
319 do
320 echo $_host
321 ssh $_host 'cat >> ~/.ssh/authorized_keys' < ~/.ssh/id_rsa.pub
322 done
323 }
324
325 # --------------------------------------------------------------------
326 # PATH MANIPULATION FUNCTIONS
327 # --------------------------------------------------------------------
328
329 # Usage: pls [<var>]
330 # List path entries of PATH or environment variable <var>.
331 pls () { eval echo \$${1:-PATH} |tr : '\n'; }
332
333 # Usage: pshift [-n <num>] [<var>]
334 # Shift <num> entries off the front of PATH or environment var <var>.
335 # with the <var> option. Useful: pshift $(pwd)
336 pshift () {
337 local n=1
338 [ "$1" = "-n" ] && { n=$(( $2 + 1 )); shift 2; }
339 eval "${1:-PATH}='$(pls |tail -n +$n |tr '\n' :)'"
340 }
341
342 # Usage: ppop [-n <num>] [<var>]
343 # Pop <num> entries off the end of PATH or environment variable <var>.
344 ppop () {
345 local n=1 i=0
346 [ "$1" = "-n" ] && { n=$2; shift 2; }
347 while [ $i -lt $n ]
348 do eval "${1:-PATH}='\${${1:-PATH}%:*}'"
349 i=$(( i + 1 ))
350 done
351 }
352
353 # Usage: prm <path> [<var>]
354 # Remove <path> from PATH or environment variable <var>.
355 prm () { eval "${2:-PATH}='$(pls $2 |grep -v "^$1\$" |tr '\n' :)'"; }
356
357 # Usage: punshift <path> [<var>]
358 # Shift <path> onto the beginning of PATH or environment variable <var>.
359 punshift () { eval "${2:-PATH}='$1:$(eval echo \$${2:-PATH})'"; }
360
361 # Usage: ppush <path> [<var>]
362 ppush () { eval "${2:-PATH}='$(eval echo \$${2:-PATH})':$1"; }
363
364 # Usage: puniq [<path>]
365 # Remove duplicate entries from a PATH style value while retaining
366 # the original order. Use PATH if no <path> is given.
367 #
368 # Example:
369 # $ puniq /usr/bin:/usr/local/bin:/usr/bin
370 # /usr/bin:/usr/local/bin
371 puniq () {
372 echo "$1" | tr : '\n' | nl | sort -u -k 2,2 | sort -n |
373 cut -f 2- | tr '\n' : | sed -e 's/:$//' -e 's/^://'
374 }
375
376 # -------------------------------------------------------------------
377 # USER SHELL ENVIRONMENT
378 # -------------------------------------------------------------------
379
380 # source ~/.shenv now if it exists
381 test -r ~/.shenv &&
382 . ~/.shenv
383
384 # condense PATH entries
385 PATH=$(puniq $PATH)
386 MANPATH=$(puniq $MANPATH)
387
388 # Use the color prompt by default when interactive
389 test -n "$PS1" &&
390 prompt_color
391
392 # -------------------------------------------------------------------
393 # MOTD / FORTUNE
394 # -------------------------------------------------------------------
395
396 test -n "$INTERACTIVE" -a -n "$LOGIN" && {
397 # Get current uname and uptime (if exists on this host)
398 # Strip any leading whitespace from uname and uptime commands
399 t_uname="$(test -n "`type -P uname`" && uname -npsr | sed -e 's/^\s+//')"
400 t_uptime="$(test -n "`type -P uptime`" && uptime | sed -e 's/^\s+//')"
401 if [ -n "$t_uname" ] || [ -n "$t_uptime" ]; then
402 echo " --"
403 test -n "$t_uname" && echo $t_uname
404 test -n "$t_uptime" && echo $t_uptime
405 echo " --"
406 fi
407 unset t_uname t_uptime
408 }
409
410 # vim: ts=4 sts=4 shiftwidth=4 expandtab