Last active
February 5, 2023 05:59
-
-
Save chmaynard/c013b570298a947a0442f99ddcf4dbd8 to your computer and use it in GitHub Desktop.
Bash slugify function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------------------ | |
# 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