Skip to content

Instantly share code, notes, and snippets.

@jamesoff
Created December 3, 2013 15:30
Show Gist options
  • Save jamesoff/7771165 to your computer and use it in GitHub Desktop.
Save jamesoff/7771165 to your computer and use it in GitHub Desktop.
zsh function to hash a string Use it for hostname colouring in your prompt!
function string_hash() {
HASHSTR=$1
HASHSIZE=$2
HASHVAL=0
for i in {1..${#HASHSTR}}; do;
THISCHAR=$HASHSTR[$i]
HASHVAL=$(( $HASHVAL + $((#THISCHAR)) ))
done
# Avoid 0 as that's black
HASHSIZE=$(( $HASHSIZE - 1 ))
HASHVAL=$(( $HASHVAL % $HASHSIZE ))
HASHVAL=$(( $HASHVAL + 1 ))
echo $HASHVAL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment