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