We're very pleased that you want to get in touch with us. Please fill in the form below:



or   Close this form  
Some content

Peter Pilgrim :: Java Champion :: Digital Developer Architect

I design Java EE and Scala software solutions for the blue-chip clients and private sector

Hey all! Thanks for visiting. I provide fringe benefits to interested readers: checkout consultancy, training or mentorship Please make enquiries by email or call +44 (0)7397 067 658.

Due to the Off-Payroll Working plan for the UK government, unfortunately, I am no longer accepting standard GOV.UK contract engagements for the public sector. Please enquire for further information.

My favourite Bash profile Linux and Mac OS X updated for 2018

19 February 2018 Comments off

10 minutes

2209

As a platform engineer, I want to keep a copy of a my favourite Linux Bash Profile in my blog on the public web, so that I do not have to keep making it up from scratch every time I configure a Virtual Machine.

## MY FAVOURITE BASH PROFILE ~/.bash_profile
### Based of the http://www.xenonique.co.uk/blog/2012/06/29/favourite-linux-and-unix-bash-aliases/


# Load workspace HTTP proxy configuration, if any
if [ -f ${HOME}/.workspace-http-proxy.sh.env ]; then
    . ${HOME}/.workspace-http-proxy.sh.env
fi

set -o emacs
# Do not exit on EOF. To exit the shell type `exit'
set -o ignoreeof
# Do not allow output redirection (>) to overwrite an existing file
set -o noclobber
# set the core limit
ulimit -c 10240

# As a platform engineer, I want to upgrade SHELL history to "remember long time"
set history=1024
set savehist=1024

## export LDFLAGS='-lpthread'
## Slackware: export LS_OPTIONS='--8bit --color=tty -F -b -T 0'
## export LS_OPTIONS='--color=tty -F -b --author'
## export LS_OPTIONS='-F'
export LS_OPTIONS='--color=tty -F'

# Aliases
alias ls="/bin/ls $LS_OPTIONS"
alias ll='ls -l'
alias lsd='ls -ld'
alias la='ls -a'
alias lf='ls -F'
alias lr='ls -alFRt'
alias lx='ls -xF'
alias llar='ls -laFR'
alias lt='ls -lartF'
alias lrt='ls -lrt'

alias home='cd ${HOME}'
alias up='cd ..'
alias up2='cd ../.'
alias up3='cd ../../..'
alias pe='printenv'
alias senv='env | sort'
alias pu='pushd'
alias po='popd'
alias pud='pushd .'
alias rot='pushd +1'
alias jobs='jobs -l'
alias mroe=more
alias lses=less
alias lsse=less
alias l=ls
alias f=file
alias c=cat
alias m=more
alias j=jobs
alias k=kill
alias d=dirs
alias h=history
alias his=history
alias hm='history | less'
alias sy3='sync; sync; sync; echo "sync 3 times ..."'
alias del='rm -i'
alias bye=exit
alias ciao=exit

## Some quick GIT alias
alias gpull='git pull origin master'
alias gpush='git push origin master'
# As a platform engineer, I want to configure Git cache to remember my password for one month on average
alias set_git_cache='git config --global credential.helper "cache --timeout=2548800" '


alias macjava=/usr/libexec/java_home
alias gpull='git pull origin $_GIT_BRANCH'
alias gpush='git push origin $_GIT_BRANCH'
alias gpull_peat='git pull peat $_GIT_BRANCH'
alias gpush_peat='git push peat $_GIT_BRANCH'

# Switch between JDK 8,9,10 and 11
function checkSetJdk() {
    if [ $# -ne 0 ]; then
        /usr/libexec/java_home -v $@ > /dev/null 2>&1
        status=$?
        if [ $status -ne 0 ]; then
            echo "*ERROR*: Sorry, this version of the JDK [$@] does not exist. (status:$status)" 1>&2
            return 1
        else
            removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
            if [ -n "${JAVA_HOME+x}" ]; then
                removeFromPath $JAVA_HOME
            fi
            export JAVA_HOME=`/usr/libexec/java_home -v $@`
            # echo -e "\n*FINAL* setting JAVA_HOME to ${JAVA_HOME}"

            # Does the older JRE folder exist
            if [ -d ${JAVA_HOME}/jre ]; then
                export PATH=$JAVA_HOME/jre/bin:$PATH
            fi
            export PATH=$JAVA_HOME/bin:$PATH
            # echo -e "\n*FINAL* setting PATH to $PATH"
        fi
    fi
 }


function removeFromPath() {
    export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
    ### echo -e "\n*DEBUG setting now to PATH=$PATH"
}

function traverseAncestorsForGitRepository()
{
    local dir=`pwd`
    local savedDir=$dir
    export _GIT_BRANCH="__UNKNOWN__"
    while [ "$dir" != "/" ]
    do
      if [ -d ".git" ]; then
          # echo "*DEBUG* Found Git repository inside  ${dir} "
          export _GIT_BRANCH=$( git branch | cut -d " " -f2)
          break
      fi
      cd ..
      dir=`pwd`
      # echo "*DEBUG* dir=$dir"
    done
    cd $savedDir
}

# This following code is only for Apple Terminal (Mac OS X)
# Check if this function: update_terminal_cwd() is defined
type update_terminal_cwd > /dev/null 2>&1; status=$?
if [ $status -eq 0 ]; then
  PROMPT_COMMAND="traverseAncestorsForGitRepository; update_terminal_cwd"
else
  PROMPT_COMMAND="traverseAncestorsForGitRepository"
fi
# FIXME: Task for me to find out the equivalent shell function to change the title bar on iTerm2 shell application.


## See this Apple Support Discussion https://discussions.apple.com/thread/4158054
alias restart-webcam='sudo killall VDCAssistant'



alias vibashpro='vi ~/.bash_profile'
alias rebashpro='. ~/.bash_profile'

## Where is GO Language installed?
export GO_HOME=/opt/go

## Where is Java installed?
export JAVA_HOME=`/usr/libexec/java_home`
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0/jdk1.8.0_161
export JAVA8_HOME=/usr/lib/jvm/jdk1.8.0/jdk1.8.0_161

export JAVAFX_HOME=${JAVA_HOME}

# Where is Scala installed (Mac OS X)?
export SCALA_HOME=/Library/opt/scala/scala-2.11.7

# Where is SBT installed (Mac OS X)?
export SBT_HOME=/Library/opt/sbt/sbt-0.13.12

# Where is Groovy installed (SDK Man)
export GROOVY_HOME=${HOME}/.sdkman/groovy/current

# Where is Ant installed (Mac OS X)?
export ANT_HOME=/Library/opt/apache-ant-1.9.6

# Where is Maven installed (Mac OS X)?
export M2_HOME=/Library/opt/apache-maven-3.3.3

# http://stackoverflow.com/questions/13161387/set-the-permgen-size-for-the-child-jvm-processes-that-maven-will-spin-off
export MAVEN_OPTS="-XX:MaxPermSize=256m -Xms512m -Xmx1024m"

# Where is Gradle installed (Mac OS X)?
export GRADLE_HOME=${HOME}/.sdkman/gradle/current

# Where is Node installed?
export NODE_PATH=/usr/local/lib/node_modules

## Where is IntelliJ installed?
export INTELLIJ_HOME=/opt/intellij-idea

## Define PATH
export PATH=.:${HOME}/bin:${HOME}/.local/bin:\
${SCALA_HOME}/bin:${SBT_HOME}/bin:\
${GROOVY_HOME}/bin:${M2_HOME}/bin:${ANT_HOME}/bin:${GRADLE_HOME}/bin:\
${GO_HOME}/bin:\
${JAVA_HOME}/bin:${JAVA_HOME}/jre/bin:\
${INTELLIJ_HOME}/bin:\
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

## Keep a copy of the original PATH
export ORIGINAL_PATH=${HOME}/bin:${HOME}/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

### This is the original PS1 on Debian (e.g Ubuntu or Linux Mint)
#### "${debian_chroot:+($debian_chroot)}\u@\h \w \$ "
export SAVED_PS1="${debian_chroot:+($debian_chroot)}\u@\h \w [\$HISTCMD] \$ "

## Ansi Color XTerm (tested on LinuxMint )
## See also https://askubuntu.com/questions/558280/changing-colour-of-text-and-background-of-terminal
#export PS1="\e[38;5;034m\u@\h \e[0m\e[38;5;039m\W\e[0m [\$HISTCMD] > "

## See also https://stackoverflow.com/questions/2024884/commandline-overwrites-itself-when-the-commands-get-too-long
case `uname -s` in
    darwin | Darwin )
        # MAC OS X
        ## See also this article: https://stackoverflow.com/questions/28782394/how-to-get-osx-shell-script-to-show-colors-in-echo
        BLACK="\033[49;1;30m"
        LTRED="\033[49;1;31m"
        LTGREEN="\033[49;1;32m"
        LTYELLOW="\033[49;1;33m"
        LTBLUE="\033[49;1;34m"
        LTMAGENTA="\033[49;1;35m"
        LTCYAN="\033[49;1;36m"
        WHITE="\033[49;1;37m"
        CLEAR="\033[0m"
        LIGHT_GRAY="\033[49;1;33m"
        ## echo "This is a Darwin (Apple Mac OS X)"
        ;;

    *)
        # This is not MAC OS X
        BLACK="\[\033[49;1;30m\]"
        LTRED="\[\033[49;1;31m\]"
        LTGREEN="\[\033[49;1;32m\]"
        LTYELLOW="\[\033[49;1;33m\]"
        LTBLUE="\[\033[49;1;34m\]"
        LTMAGENTA="\[\033[49;1;35m\]"
        LTCYAN="\[\033[49;1;36m\]"
        WHITE="\[\033[49;1;37m\]"
        CLEAR="\[\033[0m\]"
        LIGHT_GRAY="\[\033[49;1;33m\]"
        ## echo "This is not Darwin"
        ;;
esac

## Mac OS X
## See also https://stackoverflow.com/questions/2024884/commandline-overwrites-itself-when-the-commands-get-too-long
BLACK="\[\033[49;1;30m\]"
LTRED="\[\033[49;1;31m\]"
LTGREEN="\[\033[49;1;32m\]"
LTYELLOW="\[\033[49;1;33m\]"
LTBLUE="\[\033[49;1;34m\]"
LTMAGENTA="\[\033[49;1;35m\]"
LTCYAN="\[\033[49;1;36m\]"
WHITE="\[\033[49;1;37m\]"
CLEAR="\[\033[0m\]"
LIGHT_GRAY="\[\033[49;1;33m\]"


export PS1="$LTGREEN\u$LTBLUE@\h:$LIGHT_GRAY\W$CLEAR [\$HISTCMD] ❯ "
## export PS1="$LTGREEN\u$LTCYAN@\h:$LIGHT_GRAY\W$CLEAR [\$HISTCMD] ❯ "

# Add the Git Bash completion, if it exists

if [ -f ${HOME}/git-completion.bash -a -r ${HOME}/git-completion.bash ]; then
	#### echo ${HOME}/git-completion.bash (see also https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash )
	. ${HOME}/git-completion.bash
fi


# Something for Linux and Mac OS X to remember credential login for a period of timeout *PP* 01-March-2018
alias setup_git_cache='git config --global credential.helper "cache --timeout=2548800" '


# Git configuration is now inside a function. We only have to perform this set once per user login *PP*
function _configureGitAliasPerUser()
{
    ## Helpful Git aliases
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci commit
    git config --global alias.st status
    git config --global alias.unstage 'reset HEAD --'
    git config --global alias.last 'log -1 HEAD'
    git config --global alias.hdiff 'diff HEAD^ HEAD'
    git config --global alias.hd 'diff HEAD^ HEAD'


    # Show me all git commits in short form
    git config --global alias.ls 'log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%C(cyan)\\ [%cn]" --decorate'
    # Show me all git all changed files in short format
    git config --global alias.ll 'log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%C(cyan)\\ [%cn]" --decorate --numstat'
    # Show me changes list with no colours if I need to chain the out put with Unix pipes:
    git config --global alias.lnc 'log --pretty=format:"%h\\ %s\\ [%cn]"'
    # Show me changes with dates
    git config --global alias.lds 'log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%C(cyan)\\ [%cn]" --decorate --date=short'
    # Show me the history of a file, with diffs
    git config --global alias.filelog 'log -u'
    git config --global alias.fl 'log -u'
    # Show me modified files in the last commit
    git config --global alias.dl '!git ll -1'
    # Find me a file in the codebase e.g. 'git ff foo.java' or 'git ff build.gradle'
    git config --global alias.ff '!git ls-files | grep -i '
    ## Grep the entire codebase for a particular string ignore case!  e.g 'git agrep "mt103"''
    git config --global alias.agrep 'grep -Ii'

    # List all of my Git aliases, because sometimes I forget them
    git config --global alias.la '!git config -l | grep alias | cut -c 7- | sort'

    # Git stash
    git config --global alias.sl 'stash list '
    git config --global alias.sa 'stash apply '
    git config --global alias.ss 'stash save '

    ## Reset aliases
    # Reset the current HEAD to the specified state (How do I undo the most recent commit I made in Git that I have not yet pushed? 😉 *PP*
    # See  also https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git
    git config --global alias.r 'reset'
    # Reset softly to the previous checked in version
    git config --global alias.r1 'reset HEAD^'
    # Reset softly to the previous-previous checked in version
    git config --global alias.r2 'reset HEAD^^'
    # *WARNING* Advanced users only see https://stackoverflow.com/questions/9529078/how-do-i-use-git-reset-hard-head-to-revert-to-a-previous-commit
    # Perhaps, get clarification from your Anchor beforehand
    # Reset by throwing away your uncommited changes, perhaps stash them first
    git config --global alias.rh 'reset --hard'
    # Reset hard to the previous version
    git config --global alias.rh1 'reset HEAD^ --hard'
    # Reset hard to the previous-previous version
    git config --global alias.rh2 'reset HEAD^^ --hard'



    ## The next alias does not work on Santander stock self-service Git *PP* 01-March-2018
    # git config --global alias.glog log --pretty=format:"%h %s" --graph

    ## From Ales Setinc
    ##   "I found this little visual hack for "git log" command. I find it very useful, especial if you add it to your .bash_profile as a new command. Check the image attached."
    ## 🙂
    alias git-log='git log --graph --pretty="format:%C(yellow)%h%Cred%d%Creset %s %C(green)(%ar) %C(cyan)%an%Creset"'
    alias gitlog=git-log
    alias glog=git-log

}

# Here we attempt to avoid the error error: "could not lock config file /Users/xkc3198/.gitconfig: File exists"
# by using our own lock file. We also move the Git configuration to a Shell function
# *PP* 26-Mar-2018
# https://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at
_USER_GIT_CONFIGURE_LOCKFILE=${HOME}/.user-git-configure.lock
if [ -e ${_USER_GIT_CONFIGURE_LOCKFILE} ] && kill -0 `cat ${_USER_GIT_CONFIGURE_LOCKFILE}`; then
    [ "$TERM" = "xterm-256color" ] && echo "already running - lock file: ${_USER_GIT_CONFIGURE_LOCKFILE}"
else
    # make sure the lockfile is removed when we exit and then claim it
    trap "/bin/rm -f ${_USER_GIT_CONFIGURE_LOCKFILE}" INT TERM EXIT
    echo $$ > ${_USER_GIT_CONFIGURE_LOCKFILE}

    _configureGitAliasPerUser

    /bin/rm -f ${_USER_GIT_CONFIGURE_LOCKFILE}
fi


# fini

#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
[[ -s "/Users/peterpilgrim/.sdkman/bin/sdkman-init.sh" ]] && source "/Users/peterpilgrim/.sdkman/bin/sdkman-init.sh"

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

That was my user story from last week.

Addendum

I want to also share this BASH configuration in Home office and Work office situation. In work office, like you I work behind a HTTP Proxy so I extracted shell as an environment script to source-in.

## File: ~/.workspace-http-proxy.sh.env
## set the proxy connections# set the proxy connections# set the proxy connections
#export HTTP_PROXY='http://user2018:Pass2018@corporateproxy.com:8080'
#export HTTPS_PROXY='http://user2018:Pass20188@corporateproxy.com:8080'
#export NO_PROXY="localhost, 127.0.0.1, 10.24*, *corporateproxy.com, *.corp"
#export http_proxy=${HTTP_PROXY}
#export https_proxy=${HTTPS_PROXY}
#export ALL_PROXY=${HTTPS_PROXY}
#export no_proxy=$NO_PROXY

Save this ${HOME}/.workspace-http-proxy.sh.env and also execute the chmod 600 on this file.

Have fun and enjoy your day.

+PP+

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Contents of this blog entry are under copyright © 2017 by Peter Pilgrim and associates. For enquiries after republishing, please contact us for permission. All requests for syndicated content will be ignored /dev/null, consider yourself warned!

I help to design, create and build JVM components and services that are behind popular e-commerce websites.

My Blurb

Please get in touch , directly, to establish hire availability, contract & consulting opportunities.

Speaking at Your Conference

Contact by invitation

What Peter Does

Contact