Skip to content

Instantly share code, notes, and snippets.

@CodeAlDente
Created January 19, 2023 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeAlDente/d426c9fd001139d253ea368e181c49e5 to your computer and use it in GitHub Desktop.
Save CodeAlDente/d426c9fd001139d253ea368e181c49e5 to your computer and use it in GitHub Desktop.
Simple "slugify" to give filenames a unified structure to handle them
# Slugify
#
# URL: https://duncanlock.net/blog/2021/06/15/good-simple-bash-slugify-function/
#
# Transliterate everything to ASCII
# Strip out apostrophes
# Anything that's not a letter or number to a dash
# Strip leading & trailing dashes
# Everything to lowercase
function slugify() {
iconv -t ascii//TRANSLIT \
| tr -d "'" \
| sed -E 's/[^a-zA-Z0-9]+/-/g' \
| sed -E 's/^-+|-+$//g' \
| tr "[:upper:]" "[:lower:]"
}
@CodeAlDente
Copy link
Author

~$ echo "Hello World" | slugify

hello-world

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment