简体   繁体   中英

Obscure string in bash script

I am trying to do the following:

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

So that if I do:

obscure 'mysecretstring'

I get:

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

What matcher can I use for tr , instead of [:alnum:] to mean 'any character'?

Is there a better way to implement obscure ? Another option that comes to mind is sed .

You can use pure BASH:

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

"${txt//?/*}" will replace each character in $txt by *

Test it:

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

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

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

obscure '中文版'
***

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM