Skip to content

Instantly share code, notes, and snippets.

@chmaynard
Last active February 5, 2023 05:59
Show Gist options
  • Save chmaynard/c013b570298a947a0442f99ddcf4dbd8 to your computer and use it in GitHub Desktop.
Save chmaynard/c013b570298a947a0442f99ddcf4dbd8 to your computer and use it in GitHub Desktop.
Bash slugify function
#------------------------------------------------------------------------------------------
# Slugify
#
# Usage: stdin -> slugify -> stdout
# Example: $ printf "%s\n" "FYI: 'Tacit Programming' is Cool!" | slugify
# Source: https://duncanlock.net/blog/2021/06/15/good-simple-bash-slugify-function/
#
# 1. Transliterate everything to ASCII
# 2. Strip out apostrophes
# 3. Replace non-alphanumeric runs with a dash
# 4. Strip leading and trailing dashes
# 5. Everything to lowercase
#------------------------------------------------------------------------------------------
slugify()
{
iconv -t ascii//TRANSLIT \
| tr -d "'" \
| sed -E 's/[^a-zA-Z0-9]+/-/g' \
| sed -E 's/^-+|-+$//g' \
| tr "[:upper:]" "[:lower:]"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment