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