Bash Environment

useful guides to get understanding of .bash_profile, .bashrc, and .profile:

  1. Choosing between .bashrc, .profile, .bash_profile, etc
  2. Zsh/Bash startup files loading order (.bashrc, .zshrc etc.)

 

.bash_profile points to .profile and .bashrc:

if [ -f ~/.profile ]; then
  source ~/.profile
fi
if [ -f ~/.bashrc ]; then
  source ~/.bashrc
fi

 

.bashrc houses all aliases:

alias spark_cluster="ssh -i ~/.ssh/key.pem hadoop@12.34.56.789"

 

.profile has export PATHs and some useful functions:

#print CPU, RAM, and Disk usage
function system_stats {
  top -l 1 -s 0 | awk ' /usage/ ||/Processes/ || /PhysMem/ || /Load Avg/{print}'
  df -h | awk '$NF=="/"{printf "Disk Usage: %d/%dGB (%s)\n", $3,$2,$5}'
}

#mark a filepath
function mark {
  mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}

#unmark a filepath
function unmark {
  rm -i "$MARKPATH/$1"
}

#list all marks
function marks {
  if [ $MARKS_ENV = "Darwin" ]; then
    ls -l "$MARKPATH" | tail -n +2 | sed 's/ / /g' | cut -d' ' -f9- | awk -F ' -> ' '{printf "%-10s -> %s\n", $1, $2}'
  elif [ $MARKS_ENV = "Linux" ]; then
    ls -l "$MARKPATH" | sed 's/ / /g' | cut -d ' ' -f 9- | sed 's/ -/\t-/g' && echo
  fi
}

# tab completion for jump / unmark command
function _completemarks() {
local curw=${COMP_WORDS[COMP_CWORD]}
  if [ $MARKS_ENV = "Darwin" ]; then
    local wordlist=$(find $MARKPATH -type l -print | sed "s#.*/\(.*\)#\1#")
  elif [ $MARKS_ENV = "Linux" ]; then
    local wordlist=$(find $MARKPATH -type l -printf "%f\n")
  fi
  COMPREPLY=($(compgen -W '${wordlist[@]}' -- "$curw"))
  return 0
}
complete -F _completemarks jump mark unmark

 

Additional material:
Understanding the linux filesystem (/etc, /var, /bin, /opt etc.)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s