Skip to content

Instantly share code, notes, and snippets.

@josephspurrier
Last active September 13, 2022 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save josephspurrier/acf7327726df6587a56ff2c2062314fa to your computer and use it in GitHub Desktop.
Save josephspurrier/acf7327726df6587a56ff2c2062314fa to your computer and use it in GitHub Desktop.
macOS Bash Profile
#################################################################################
#
# Bash Configurations and Aliases for OS X
#
# Latest: https://gist.github.com/josephspurrier/acf7327726df6587a56ff2c2062314fa
# This was modified so it can be included in your ~/.zshrc file with the command:
# source ~/.bash_profile
#
# It's recommended to use oh-my-zsh:
# sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
#
################################################################################
################################################################################
# Terminal
################################################################################
# Preferred iTerm2 Theme
# https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/schemes/NightLion%20v1.itermcolors
# Set colorized terminal prompt
# Prompt: Hostname:current_dir
#export PS1="\[\033[36m\]\h\[\033[m\]:\[\033[32m\]\w\[\033[m\] "
# Bash completion.
## If running Bash 3.2 included with macOS
# brew install bash-completion
## or, if running Bash 4.1+
# brew install bash-completion@2
#[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
#if [ -f $(brew --prefix)/etc/bash_completion ]; then
#. $(brew --prefix)/etc/bash_completion
#fi
################################################################################
# Filesystem
################################################################################
# List files with color
alias ls='ls -GFh'
# Default editor
export EDITOR="/usr/bin/nano"
# Open Finder for current directory
alias finder='open -a Finder ./'
# Modify cd to list folder contents when directory is changed
# Usage: cd
cd() { builtin cd "$@"; ls; }
# Traverse folders quicker
alias cd2='cd ../../' # Go back 2 directory levels
alias cd3='cd ../../../' # Go back 3 directory levels
alias cd4='cd ../../../../' # Go back 4 directory levels
alias cd5='cd ../../../../../' # Go back 5 directory levels
alias cd6='cd ../../../../../../' # Go back 6 directory levels
# Watch a file
# Pass: file to watch
# Usage: watch [string]
alias watch="less +F"
# Recursive list - easy to view individual folders
alias lsr1='ls -R | grep ":$" | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\'' | less'
# Recursive list - easy to view folders only
alias lsr2='find . -type d -not -path "*/\.*"'
# Recursive list - easy to view files only
alias lsr3='find . -type f -not -path "*/\.*"'
# Recursive list - easy to view folders and files
alias lsr4='find . -not -path "*/\.*"'
################################################################################
# Networking
################################################################################
# Internal IP
alias iip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | awk '{print \$2}'"
# External IP
alias eip="dig +short myip.opendns.com @resolver1.opendns.com"
# Gateway IP
alias gip="route get default | grep "gateway:" | awk '{print \$2}'"
# Flush DNS
alias flushdns='dscacheutil -flushcache'
# Ping with timestamps
alias pingt='ping google.com | while read pong; do echo "$(date): $pong"; done'
# Speedtest
# Source: https://gist.github.com/josephspurrier/b136becee4e42c7eeac5efc5289c41a8
alias speedtest='curl -s https://gist.githubusercontent.com/josephspurrier/b136becee4e42c7eeac5efc5289c41a8/raw/529dfc09c217bb7f8026e9744d4188de837d9d9d/speedtest.py | python -'
################################################################################
# Security
################################################################################
# Don't add commands that start with a space to bash history
export HISTIGNORE=" *"
# Base64 encode
# Pass: string to encode
# Usage: b64e [string]
b64e() {
echo -n "$1" | base64
}
# Base64 decode
# Pass: string to decode
# Usage: b64d [string]
b64d() {
echo -n "$1" | base64 -D
echo ""
}
# Lock screen
alias lock='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'
# Generate password
# Pass: length of password to generate
# Usage: pwg [int]
pwg() {
cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | fold -w $1 | head -n 1
}
# Current machine information
# Usage: ii
ii() {
echo -e "\nAdditional information:$NC " ; uname -a
echo -e "\nUsers logged on:$NC " ; w -h
echo -e "\nCurrent date :$NC " ; date
echo -e "\nMachine stats :$NC " ; uptime
echo -e "\nPublic facing IP Address :$NC " ;eip
#echo -e "\nDNS Configuration:$NC " ; scutil --dns
echo
}
# Show the fingerprint of a private key to compare to AWS public key fingerprint.
fingerprint-private() {
openssl pkcs8 -in $1 -nocrypt -topk8 -outform DER | openssl sha1 -c
}
# Show the fingerprint of a public key to compare to AWS public key fingerprint.
fingerprint-public() {
openssl pkey -pubin -in $1 -pubout -outform DER | openssl md5 -c
}
# Generate a public key from a private key.
pubkey-generate() {
openssl pkey -in $1 -pubout
}
# Generate the OpenSSH key from a private key.
sshkey-generate() {
ssh-keygen -y -f $1
}
################################################################################
# AWS
################################################################################
# Convert AWS Secret Access Key to an Amazon SES SMTP password.
# Pass in the AWS Access Key password.
# Source: https://gist.github.com/memiah-steve/593c2026782309367f1e1edfef07f9af
# http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html
sespass() {
if [ "$#" -ne 1 ]; then
echo "Usage: ./aws-ses-smtp-password.sh secret-key-here"
exit 1
fi
KEY="${1}"
MESSAGE="SendRawEmail"
VERSION_IN_BYTES=$(printf \\$(printf '%03o' "2"));
SIGNATURE_IN_BYTES=$(echo -n "${MESSAGE}" | openssl dgst -sha256 -hmac "${KEY}" -binary);
SIGNATURE_AND_VERSION="${VERSION_IN_BYTES}${SIGNATURE_IN_BYTES}"
SMTP_PASSWORD=$(echo -n "${SIGNATURE_AND_VERSION}" | base64);
echo "${SMTP_PASSWORD}"
}
################################################################################
# Miscellaneous
################################################################################
# Show path variables
alias path='echo -e ${PATH//:/\\n}'
# Reload .bash_profile
alias reload=". ~/.bash_profile; echo -e 'Profile reloaded.'"
# Start PHP webserver
# Pass: folder to serve
# Usage: phpw [folder]
alias phpw="php -S localhost:8080 -t"
# Python webserver
alias http="python -m SimpleHTTPServer 8080"
# Node webserver with HTTPS
# sudo npm install -g http-server
# Create SSL key: https://certsimple.com/blog/localhost-ssl-fix
alias https="http-server --ssl --cert ~/Localhost.crt --key ~/Localhost.pem"
# Extract any archive
# Pass: file to extract
# Usage: extract [file]
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# Split iTerm in to 4 panes.
alias split4='osascript ~/split4.scpt'
################################################################################
# Homebrew
################################################################################
# Mono Framework
export MONO_GAC_PREFIX="/usr/local"
# Add Brew sbin folder to path
# Ensure path is only added once when reloading
if [[ $PATH != *"/usr/local/sbin:"* ]]; then
export PATH=/usr/local/sbin:$PATH
fi
################################################################################
# Go
################################################################################
# Go Workspace and Root
export GOPATH=$HOME/workspace
#export GOROOT=/usr/local/go1.8.1
# Add Go bin folder to path
# Ensure path is only added once when reloading
#if [[ $PATH != *":$GOPATH/bin"* ]]; then
#export PATH=$PATH:$GOPATH/bin
#fi
# Easily test all folders except vendor
alias gotest='go test $(go list ./... | grep -v vendor/)'
# Set the GOPATH to the current directory
alias gop='export GOPATH=$(pwd)'
# Add the current directory to path
alias pathx='export PATH=$PATH:$(pwd)'
# Output the gopath
alias gopath='echo $GOPATH'
# Show the coverage heatmap
alias gocover='go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html && open cover.html && sleep 5 && rm cover.html && rm cover.out'
################################################################################
# Docker
################################################################################
# To enable docker bash completion, run these commands once:
# cd /usr/local/etc/bash_completion.d
# ln -s /Applications/Docker.app/Contents/Resources/etc/docker.bash-completion
# ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.bash-completion
# ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.bash-completion
# Remove dangling Docker images.
alias docker-rmid='docker rmi -f $(docker images --filter "dangling=true" -q)'
# Remove old containers.
alias docker-rm='docker rm -f $(docker ps -a -q)'
# Set docker to docker-machine default environment.
alias docker-init='eval $(docker-machine env default)'
# Set docker config to use minikube environment.
alias docker-minikube='eval $(minikube docker-env)'
# Unset the docker environment.
alias docker-unset='eval $(docker-machine env -u)'
# Use Azure CLI from docker.
# Start from makefile: docker run -d -t --name azurecli -v $(shell pwd):/root mcr.microsoft.com/azure-cli
# Stop from makefile: docker rm -f azurecli
az() {
docker exec azurecli az "$@"
}
################################################################################
# Kubernetes
################################################################################
# Add minikube completion.
# Will have to reload this profile after the first time.
# I'd suggest commenting this out after you uncomment it and run it.
#minikube completion bash > /usr/local/etc/bash_completion.d/minikube-completion
# Set kubectl to kube for easy typing.
alias kube='kubectl'
# Bug: https://github.com/kubernetes/kubectl/issues/120#issue-270604886
#complete -F __start_kubectl kube
# Set minikube to mkube for easy typing.
alias mkube='minikube'
# Set kubectl to use the default Kubernetes config. Could simply unset as well.
alias kube-default='export KUBECONFIG=~/.kube/config'
# Set kubectl to use an alternative Kubernetes config for minikube.
alias kube-minikube='export KUBECONFIG=~/.kube/config-minikube'
# Get a pod name quickly since it contains unique characters at the end.
# Example: kpod hello-world
# Example: `kpod hello-world`
kpod() {
kubectl get pods | grep $1 | awk {'print $1'}
}
# Execute a command on the pod easily. It uses kpod to accept a short pod name.
# Example: kexec hello-world sh
# Example: kexec hello-world-566897b7d9-sq8q5 bash
kexec() {
kubectl exec -it `kpod $1` $2
}
# Get the logs from a pod easily. It uses kpod to accept a short pod name.
# Example: klog hello-world
# Example: klog hello-world-566897b7d9-sq8q5 -f
klog() {
kubectl logs $2 `kpod $1`
}
################################################################################
# NPM
################################################################################
# Add the current NPM bin folder to PATH.
alias pathnpm='export PATH=$PATH:$(npm bin)'
################################################################################
# Other Applications
################################################################################
# Add VSCode to PATH.
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
# Open iTerm.
alias mts="osascript ~/split3.scpt"
################################################################################
# Reference
################################################################################
# BASH Examples: https://natelandau.com/my-mac-osx-bash_profile/
# iTerm2 Colors: https://github.com/mbadolato/iTerm2-Color-Schemes
#!/usr/bin/osascript
tell application "iTerm2"
tell current session of current tab of current window
split vertically with default profile
split vertically with default profile
split vertically with default profile
end tell
tell second session of current tab of current window
write text "/Users/josephspurrier/godev/gomithriltsapp"
write text "make db-rm"
write text "sleep 2"
write text "make db-init"
write text "sleep 15"
write text "make api-dev"
end tell
tell third session of current tab of current window
write text "/Users/josephspurrier/godev/gomithriltsapp"
write text "make ui-dev"
end tell
tell fourth session of current tab of current window
write text "/Users/josephspurrier/godev/gomithriltsapp"
write text "make npm run storybook"
end tell
end tell
#!/usr/bin/osascript
tell application "iTerm2"
tell current session of current tab of current window
split horizontally with default profile
split vertically with default profile
end tell
tell third session of current tab of current window
split vertically with default profile
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment