用纯粹的 bash 创建 URL 友好的鼻涕虫?

Create URL friendly slug with pure bash?

我正在寻找 "slugify" 变量的纯 bash 解决方案,它不像我的那样难看。

slugify: lowercased, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -. No leading / trailing -. A string suitable to use in URL hostnames and domain names is the result. An input is most likely a series of words with undesired characters in throughout such as:

'Effrafax_mUKwT'uP7(Garkbit<}@NJ"RJ"Hactar*S;-H%x.?oLazlarl(=Zss@c9?qick.:?BZarquonelW{x>g@'k'

Of which a slug would look like: 'effrafax-mukwt-up7-garkbit-1-njrjhactar-s-h-x-olazlarl-zss-c9-q'

slugify () {
  next=${1//+([^A-Za-z0-9])/-}
  next=${next:0:63}
  next=${next,,}
  next=${next#-}
  next=${next%-}
  echo $next
}

还有为什么${next//^-|-$}不去掉前缀和后缀'-'?其他建议?

我正在使用此功能,在我的 bash 个人资料中:

slugify () {
    echo "" | iconv -t ascii//TRANSLIT | sed -r s/[~\^]+//g | sed -r s/[^a-zA-Z0-9]+/-/g | sed -r s/^-+\|-+$//g | tr A-Z a-z
}

基于:https://gist.github.com/oneohthree/f528c7ae1e701ad990e6

OS X 和 linux

的兼容变体
slugify () {
    echo "" | iconv -c -t ascii//TRANSLIT | sed -E 's/[~^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$//g' | tr A-Z a-z
}