Skip to content

Instantly share code, notes, and snippets.

@jeandat
Created January 16, 2017 14:23
Show Gist options
  • Save jeandat/683662b9bac84b016722334d269d6139 to your computer and use it in GitHub Desktop.
Save jeandat/683662b9bac84b016722334d269d6139 to your computer and use it in GitHub Desktop.
Pre & Post hook for bash prompt (terminal)

PRE / POST Hook for Bash Prompt

Explained here.

AT_PROMPT=1 
# This will run before any command is executed.
function PreCommand() {
  if [ -z "$AT_PROMPT" ]; then
    return
  fi
  unset AT_PROMPT

  # Do stuff.
  echo "Running PreCommand"
}
trap "PreCommand" DEBUG

# This will run after the execution of the previous full command line.  We don't
# want it PostCommand to execute when first starting a bash session (i.e., at
# the first prompt).
FIRST_PROMPT=1
function PostCommand() {
  AT_PROMPT=1

  if [ -n "$FIRST_PROMPT" ]; then
    unset FIRST_PROMPT
    return
  fi

  # Do stuff.
  echo "Running PostCommand"
}
PROMPT_COMMAND="PostCommand"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment