Skip to content

Instantly share code, notes, and snippets.

@LovingMelody
Last active July 20, 2017 12:37
Show Gist options
  • Save LovingMelody/338108b7d066e5d538155bd89f3b5973 to your computer and use it in GitHub Desktop.
Save LovingMelody/338108b7d066e5d538155bd89f3b5973 to your computer and use it in GitHub Desktop.
Basic bash configuration tutorial. (no real explination)

Bash Tab Completion

By default on OSX bash is missing a lot of the tab compltion.

NOTICE: run bash first before following this guide, not all shells are POSIX compatible.

Checking for brew

First of you need to have brew installed run this command in terminal

command -v brew >/dev/null 2>&1 && echo "Brew is installed." || { echo >&2 "Brew is not installed."; }

if brew is not installed follow a brew install guide.

Installing bash

Install bash, the command below will check if bash is installed and if not, it will install it.

# Install bash
(brew list | grep -Fwx "bash" > /dev/null 2>&1 ) && echo "Bash is already installed" || { brew install bash; }

# Add bash to /etc/shells
(cat /etc/shells | grep -Fwx "$(brew --prefix bash)"/bin/bash > /dev/null 2>&1) || { echo "$(brew --prefix bash)"/bin/bash | sudo tee -a /etc/shells; }

# Change default shell
chsh -s "$(brew --prefix bash)"/bin/bash

Installing bash-completion

(brew list | grep -Fwx "bash-completion@2" >/dev/null 2>&1) || { brew install bash-completion@2; }

Add the following to your ~/.bash_profile

if [ -f /usr/local/share/bash-completion/bash_completion ]; then
    . /usr/local/share/bash-completion/bash_completion
fi

Bonus: Bash git prompt

this will make your git life a bit easier

run the following commands to install

# Install bash-git-prompt
(brew list | grep -Fxw "bash-git-prompt" > /dev/null 2>&1) || { brew install bash-git-prompt; }
Add the following to your ~/.bash_profile
GIT_PROMPT_ONLY_IN_REPO=1
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
    __GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
    source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
#!/usr/bin/env bash
function prompt_user {
read -p "$1 (y/n)?" choice
case "$choice" in
y|Y ) return;;
n|N ) echo "Cancelling..."; exit 1;;
* ) echo "invalid response, cancelling"; exit 1;;
esac
}
# Check for brew
command -v brew >/dev/null 2>&1 && echo "Brew is installed." || { echo >&2 "Brew is not installed and is required."; open "https://coolestguidesontheplanet.com/installing-homebrew-on-macos-sierra-package-manager-for-unix-apps/"; exit 1; }
prompt_user "Would you like to install bash"
# Install bash
(brew list | grep -Fwx "bash" > /dev/null 2>&1 ) && echo "Bash is already installed" || { brew install bash; }
# Add bash to /etc/shells
(cat /etc/shells | grep -Fwx "$(brew --prefix bash)"/bin/bash > /dev/null 2>&1) || { echo "$(brew --prefix bash)"/bin/bash | sudo tee -a /etc/shells; }
prompt_user "Would you like to set bash as your default shell"
# Change default shell
chsh -s "$(brew --prefix bash)"/bin/bash
(brew list | grep -Fwx "bash-completion@2" >/dev/null 2>&1) || { brew install bash-completion@2; }
# Add bash-completion to ~/.bash_profile
cat >> "$HOME"/.bash_profile << EOL
if [ -f /usr/local/share/bash-completion/bash_completion ]; then
. /usr/local/share/bash-completion/bash_completion
fi
EOL
prompt_user "Would you like to install bash-git-prompt"
# install bash-git-prompt
(brew list | grep -Fxw "bash-git-prompt" > /dev/null 2>&1) || { brew install bash-git-prompt; }
# Add bash-git-prompt to ~/.bash_profile
cat >> "$HOME"/.bash_profile << EOL
GIT_PROMPT_ONLY_IN_REPO=1
if [ -f "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh" ]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
EOL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment