Linux tr command not working properly

I have this password generator:

pass () {
    # Generates a random password
    local size="${1:-12}"
    local alphabet="$2"
    </dev/urandom tr -dc "$alphabet" | head -c$size ; echo ""
}

What works fine:

» pass 20 '[:alnum:]'
DpEf8bMp7zfkvSoudItS

But the following fails:

» pass 20 '[:alnum:]@#%+-/~'
JSNweE,.EU+P.l5nqkzd

The team trclearly says:

delete all characters that do not belong to this set

Thus, the symbols ,and .are unexpected.

Where did they come from?

+3
source share
1 answer

There is also another part of the trman page that you skipped:

char1-char2

all characters from CHAR1 to CHAR2 in ascending order

Thus, some +-/will mean symbols +, ,, -, .and /. ( man asciiuseful here).

For a hyphen, you can avoid this:

pass 20 '[:alnum:]@#%+\-/~'

\055 :

pass 20 '[:alnum:]@#%+\055/~'

:

pass 20 '[:alnum:]@#%+/~-'
+2

Source: https://habr.com/ru/post/1695851/


All Articles