Last active
September 9, 2017 19:49
-
-
Save mckern/743df4643718d402034c to your computer and use it in GitHub Desktop.
This is my .bash_profile. Please consider it a useful reference. Not guaranteed to be completely current or completely quick. I use long-lived shell sessions, so a 2-second load time doesn't bother me.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Set up handy aliases, simple environment tweaks, and other pleasantries. | |
# While shooting yourself in the foot is always a possibility, you may as well | |
# do it in comfort and occasionally style. | |
# Owner: Ryan McKern | |
# Start counting how long it takes to source this profile | |
__begin="$(date +"%s")" | |
# Keep so, so, so much history | |
export HISTFILESIZE='' | |
export HISTSIZE='' | |
# Don't keep duplicates in history; keep more history! | |
export HISTCONTROL="ignorespace:erasedups" | |
# Ignore backgrounding, mutt, quitting, and clearing | |
export HISTIGNORE="&:mutt:[bf]g:exit:clear" | |
# Append every command to history | |
PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}" | |
# Avoid overwriting history | |
shopt -s histappend | |
# Smart handling of multi-line commands | |
shopt -s cmdhist | |
# check the window size after each command and, if necessary, | |
# update the values of LINES and COLUMNS. | |
shopt -s checkwinsize | |
# Set the default umask to group-writable | |
umask 0022 | |
# This is the minimum viable Bash version. | |
# Anything older than this will be used as the cut-off point. | |
__minimum_viable='3' | |
# Don't do anything for non-interactive shells | |
[[ -z "${PS1}" ]] && return | |
# Set the old Gentoo default prompt if this isn't a dumb terminal | |
if [ "${TERM}" != 'dumb' ] && [ -n "${BASH}" ]; then | |
if [ "${UID}" -eq "0" ] ; then | |
# Privileged prompt, ending in # | |
export PS1='\[\033[01;31m\]\h \[\033[01;34m\]\W # \[\033[00m\]' | |
else | |
# Unprivileged prompt, ending in $ | |
export PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]' | |
fi | |
fi | |
# Are we using something greater than bash 2? | |
if (( ! "${BASH_VERSINFO[0]}" > "${__minimum_viable}" )); then | |
echo "You're using an old version of Bash (${BASH_VERSION}). Sourcing .bash_profile will now halt." >&2 | |
return | |
fi | |
# define pathmunge, and use that for $PATH manipulation. | |
# pathmunge is normally provided in RHEL environments, | |
# and this is copied almost wholesale from their implementation. | |
pathmunge(){ | |
if ! echo "${PATH}" | grep -q "(^|:)${1}(\$|:)"; then | |
[[ -d ${1} ]] || return | |
if [ "${2}" = "after" ] ; then | |
PATH="${PATH}:${1}" | |
else | |
PATH="${1}:${PATH}" | |
fi | |
fi | |
} | |
# Use this as short-hand to determine if a function | |
# is already implemented | |
function_defined(){ | |
if [[ ${1:-UNDEFINED} == 'UNDEFINED' ]]; then | |
echo 'no function name specified' >&2 | |
return 2 | |
fi | |
type "${1}" | head -n1 | grep -q 'is a function' | |
retval=$? | |
if [[ ${retval} == 0 ]]; then | |
echo "true" | |
return | |
fi | |
echo 'false' | |
return 1 | |
} | |
# At least try to make a good faith effort to determine if | |
# core utilities are their GNU or BSD variants | |
is_gnu(){ | |
# false if utility doesn't exist | |
if ! which "${1}" > /dev/null 2>&1; then | |
echo "false" | |
return 1 | |
fi | |
# true if utility exists and says it's GNU | |
if "${1}" --version 2>&1 | grep -qi 'gnu'; then | |
echo "true" | |
return 0 | |
fi | |
# false otherwise | |
echo "false" | |
return 1 | |
} | |
# Source system-wide bash completion if it exists | |
[ -f '/etc/bash_completion' ] && source '/etc/bash_completion' | |
# This is an OS Specific switchyard: figure out what | |
# environment this is being run in, and configure it | |
# accordingly. Currently supports Solaris, OS X & Linux. | |
case "$(uname -s)" in | |
# All linux configuration goes here | |
Linux*) | |
## Have we got local bin paths? Prepend them. | |
pathmunge /usr/local/sbin before | |
pathmunge /usr/local/bin before | |
# If we're using a RHEL-based distro, let's make looking for packages easier | |
[ -f /etc/redhat-release ] && { | |
rpmgrep(){ | |
rpm -qa | grep -i "${1}" | sort | |
} | |
} | |
;; | |
# This is for the 3 or 4 solaris machines you might encounter | |
SunOS*) | |
# Seriously. I want to know. | |
echo -e "\n\nWhere did you find a Solaris machine?\n\n" | |
# Don't even bother with whatever path we've inherited. Build a new one. | |
# Make sure that pathmunge is a function, not some weird whoknowswhat | |
if type pathmunge | grep -q function; then | |
pathmunge /bin before | |
pathmunge /usr/bin before | |
pathmunge /sbin after | |
pathmunge /usr/sbin after | |
pathmunge /usr/xpg4/bin before | |
pathmunge /usr/local/bin before | |
pathmunge /usr/local/sbin before | |
pathmunge /opt/SUNWspro/bin before | |
pathmunge /usr/ccs/bin before | |
pathmunge /hub/SunOS/5.8/sun4u/apps/openssh-3.0p1/bin before | |
fi | |
# Again, let's at least look for bash completion | |
[ -f '/usr/local/etc/bash_completion' ] && source '/usr/local/etc/bash_completion' | |
# If the TERM isn't something sane, lie | |
[[ ${TERM} =~ xterm-color ]] && export TERM=xterm | |
# Make sure that CC points to a GCC if possible. That'll matter. | |
command -v gcc >/dev/null 2>&1 && export CC=gcc | |
;; | |
# This is for OS X. It's old, but it supports Homebrew | |
# and old Developer Tools installations. It also understands | |
# where Oracle installed MySQL by default. | |
Darwin*) | |
# Here's some paths that might exist. If they do, we wants them so badly! | |
pathmunge /opt/local/sbin before | |
pathmunge /opt/local/bin before | |
pathmunge /usr/local/sbin before | |
pathmunge /usr/local/bin before | |
pathmunge /Developer/Tools after | |
pathmunge /usr/local/mysql/bin after | |
# Gotta clear that DNS cache somehow, right? | |
flushdns(){ | |
sudo killall -HUP mDNSResponder | |
return $? | |
} | |
# View man pages as PDF files | |
pman(){ | |
command man -t "${@}" | open -g -f -a /Applications/Preview.app | |
return $? | |
} | |
# Don't bother with `locate` or any findutils nonsense. Just use Spotlight. | |
alias locate='mdfind -name' | |
;; | |
esac | |
###### Aliases & Functions: make your life easier ###### | |
# find dead symlinks in the current directory or $1 | |
deadlinks(){ | |
local __path="${1:-./}" | |
find -L "${__path}" -type l -print | |
} | |
# sorted list of the contents of PWD or $1 | |
alias howbig='du -Lh --max-depth=1' | |
# General aliases | |
alias ..='cd ..' | |
alias mkdir='mkdir -p' | |
alias du='du -h' | |
alias df='df -Th' | |
# generic psgrep | |
alias psgrep="ps auxww | grep -i" | |
# generic history grep | |
alias hgrep="history | grep -i" | |
# Rehash the path using the principle of least surprise | |
alias rehash="hash -r" | |
# Load everything in ~/.profile.d if it exists | |
__counter=0 | |
__profiles="${HOME}/.profile.d" | |
if [ -d "${__profiles}" ]; then | |
shopt -s nullglob | |
# Pre files | |
for __file in "${__profiles}"/pre_*.sh; do | |
source "${__file}" | |
__counter=$((__counter + 1)) | |
done | |
# Anything not tagged pre or post | |
for __file in "${__profiles}"/*.sh; do | |
[[ "$(basename "${__file}")" =~ ^pre_|post_ ]] && continue | |
source "${__file}" | |
__counter=$((__counter + 1)) | |
done | |
# Post files | |
for __file in "${__profiles}"/post_*.sh; do | |
source "${__file}" | |
__counter=$((__counter + 1)) | |
done | |
shopt -u nullglob | |
fi | |
__dim='\e[2m' | |
__normal='\e[22m' | |
# Wrap up counting the seconds since this started | |
__end="$(date +"%s")" | |
# Dump to screen and let me get on with my life! | |
printf "%bProfile loaded in %s seconds%b\n" "${__dim}" "$((__end-__begin))" "${__normal}" | |
printf "%b> Loaded %s sub-profiles from %s%b\n" "${__dim}" "${__counter}" "${__profiles}" "${__normal}" | |
unset __counter __profiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Last login: Sat Sep 9 02:18:21 on ttys000 | |
Profile loaded in 3 seconds | |
> Loaded 19 sub-profiles from /Users/ryan/.profile.d | |
ryan@bender ~ $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment