Skip to content

Instantly share code, notes, and snippets.

@codemedic

codemedic/README.md

Last active Jun 6, 2019
Embed
What would you like to do?
Linux-like terminal in Mac OS

Linux-like terminal in Mac OS

Caveat: This was done in anger to aleviate my pain; There could be a better way to do this. Please do leave comments and suggestions.

This script is meant to be used as .bashrc, in order to make the transition from Linux to MacOS less painful. It makes available GNU tools and utils in your PATH so that they can be invoked effortlessly. It also allows you to leave an untainted terminal setup for anything very native to MacOS.

This script works in cooperation with the shell application (iTerm2 or another) to provide the "bootstrap hint" to decide whether to load native setup or the brew / GNU setup. This is done by configuring your favourite terminal app to load the bash version installed via brew, leaving the native Apple Terminal app unchanged. This allows for for anything that requires pristine Apple shell environment (for what ever reason), readily available.

# Add all your favourite GNU packages (brew solution names) here
gnu_brew_pkgs=(gawk coreutils gnu-indent gnu-sed gnu-tar grep)
# Detect if we are in brewed-bash
#
# At the moment Apple distributes an archaic version (3.x) in comparison to 5.x
# from brew, which works in our favour. Till apple upgrades bash, we are safe.
brewed_bash() {
[[ "$BASH_VERSION" =~ ^5\..* ]]
}
# If we are in brewed bash, setup completion, PATH and MANPATH for the selected GNU tools
if brewed_bash; then
# add
PATH="/usr/local/bin:$PATH"
# loop through and add to PATH and MANPATH
for p in "${gnu_brew_pkgs[@]}"; do
if [ -d "/usr/local/opt/${p}" ]; then
PATH="/usr/local/opt/${p}/libexec/gnubin:$PATH"
MANPATH="/usr/local/opt/${p}/libexec/gnuman:$MANPATH"
fi
done
# export them
export PATH
export MANPATH
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
fi
# setup ls colours to work with gnu as well as darwin versions
if [[ "$(which ls)" =~ /coreutils/libexec/gnubin ]]; then
. <(dircolors)
alias ls='ls --color=auto'
else
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -Gh'
fi
alias ll="ls -la"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.