Bash Profile for Mac Users

The .bash_profile file is a hidden file that is located in your user’s home directory. This file is loaded before the Terminal application loads and contains user preferences for the command line interface, such as setting up aliases and enabling the colour prompt.

There may be occasions where this file has not been created for some reason. If this is the case, simply open up Terminal cd into your home directory by typing cd ~ and then typing touch .bash_profile to create the file.

To enable colours within Terminal, you can add the following into the .bash_profile file.

#make sure to run: git config --global color.ui true

function parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\*\1/'
}

case "$TERM" in
  xterm-*color) color_prompt=yes;;
esac


if [ "$color_prompt" = yes ]; then
    PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0;33m\]$(parse_git_branch)\[\033[00m\]\$ '
else
    PS1='\u@\h:\w$(parse_git_branch)\$ '
fi

export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad

This is also useful if you are using Git, as Terminal will also show you what working branch you are currently on.

You can also set up aliases for common commands by using the alias keyword to quickly run commands that would otherwise take unnecessary time to type.

Note that the aliases can also be stored within a .bash_aliases file, should you have the following code snippet within .bash_profile.

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
 
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

A small list of useful Mac command aliases can be seen below.

# cd command aliases
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'

# ls command aliases
alias ll='ls -la'

# Search for a file
alias qfind='find . -name '

# Create an alias to connect to a server via SSH
alias server_name='ssh username@ip_address'

# Use PHP from MAMP rather than OSX
alias php='/Applications/MAMP/bin/php/php7.0.12/bin/php'
alias composer='/Applications/MAMP/bin/php/php7.0.12/bin/php /usr/local/bin/composer'

# File archiving
alias tgz='tar -zxvf'