bash 脚本中的模糊字符串

Obscure string in bash script

我正在尝试执行以下操作:

obscure ( ) {
    local txt=""
    echo "$txt" | tr '[:alnum:]' '*'
}

所以如果我这样做:

obscure 'mysecretstring'

我得到:

**************

我可以使用什么匹配器来表示 tr,而不是 [:alnum:] 来表示 'any character'?

有没有更好的实现方式obscure?想到的另一个选项是 sed

你可以使用纯 BASH:

obscure() {
   local txt=""
   echo "${txt//?/*}"
}

"${txt//?/*}"$txt 中的每个字符替换为 *

测试一下:

obscure 'mysecretstring3123213213'
************************

obscure mysecretstring
**************

obscure '!@#$%^&*()_+=-'
**************

obscure '中文版'
***