Skip to content

Instantly share code, notes, and snippets.

@neozenith
Created March 13, 2018 02:26
Show Gist options
  • Save neozenith/7448606fc44d78b6edf7c28473407f13 to your computer and use it in GitHub Desktop.
Save neozenith/7448606fc44d78b6edf7c28473407f13 to your computer and use it in GitHub Desktop.
Bash script helper function to colorise the verbose header output of curl
# Add this to your .bash_profile
ccurl() {
# Define escape code character
ESC_CODE=""
if [[ $OSTYPE == darwin* ]]; then
ESC_CODE=`echo -e "\033"`
else
ESC_CODE=`echo -e "\e"`
fi
# Define some sensible names for the palette
RED="$ESC_CODE\[31m"
GREEN="$ESC_CODE\[32m"
YELLOW="$ESC_CODE\[33m"
BLUE="$ESC_CODE\[34m"
PURPLE="$ESC_CODE\[36m"
LRED="$ESC_CODE\[91m"
LGREEN="$ESC_CODE\[92m"
LYELLOW="$ESC_CODE\[93m"
LBLUE="$ESC_CODE\[94m"
LPURPLE="$ESC_CODE\[96m"
NORM="$ESC_CODE\[0m"
# curl verbose header information is piped to stderr
# We need to pipe stderr (2>) to stdout (1>) using >&
# Apply colourising pattern matchers
curl -sv $1 2>&1 | \
sed -E "s/^(>) (.*): (.*)$/$RED\1 $LBLUE\2: $LRED\3$NORM/g" | \
sed -E "s/^(<) (.*): (.*)$/$GREEN\1 $LBLUE\2: $LGREEN\3$NORM/g" | \
sed -E "s/^(\*) (.*)$/$PURPLE\1 $NORM\2$NORM/g" | \
sed -E "s/^(<) (.*)$/$GREEN\1 $GREEN\2$NORM/g" | \
sed -E "s/^(>) (.*)$/$RED\1 $RED\2$NORM/g"
}
# Export bash function for use in bash commandline
export -f ccurl
# USAGE:
# ccurl http://www.google.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment