There are several ways to parse out the actual digest out from the output of md5sum
:
grep -E -o '[[:alnum:]]+'
, this would return the alphanumeric part of the md5sum
output, but would also give you any filename bits if present in the output (not in the case presented in the question, as the filename is -
, standard input).
cut -d ' ' -f 1
, this would simply return the bit of the output before the first space character. This is arguably the most common way to get at the "naked" MD5 digest string.
- Variations on the
cut
theme includes awk '{ print $1 }'
and similar things.
For password generation, I would suggest using pwgen
rather than an MD5 digest, mostly because an MD5 digest, unless computed over random data, is not random.
A hack would be to use something like
tr -dc '[:alnum:]' </dev/urandom | dd bs=1 count=32 2>/dev/null
This would extract a stream of alphanumeric characters from /dev/urandom
and dd
would cut the stream off after 32 such characters. Instead of dd
, one could also use head -c 32
.
Or, using md5sum
(in this case over 1Kb of random data from /dev/urandom
):
dd if=/dev/urandom bs=1k count=1 | md5sum | cut -d ' ' -f 1
But really, just install and use pwgen
.
See also When to use /dev/random vs /dev/urandom
tr -dc '[:alnum:]' </dev/urandom | dd bs=1 count=16 2>/dev/null
– Kusalananda Nov 02 '17 at 10:12grep
(one of the few that support that non-standard-o
option and only recently), that would run indefinitely as that[a-z0-9]*
regexp matches the empty string. – Stéphane Chazelas Nov 02 '17 at 10:35