I'm trying to generate a random hex number with an specified length ($length
) using the following command:
head -c $length /dev/urandom | xxd -p -u -c $length | tr -d '[:space:]\\'
I've noticed that head -c $length /dev/urandom
actually prints the double of the $length
value. So if $length=1
the output will be 2 characters long. How can I fix this?
Also why does the input number on xxd -p -u -c $length
seems not to affect on the output? meaning I can use either:
head -c 4 /dev/urandom | xxd -p -u -c 20000 | tr -d '[:space:]\\'
or
head -c 4 /dev/urandom | xxd -p -u -c 4 | tr -d '[:space:]\\'
and it will print 8 characters in both cases.