How to get a random string of 32 hexadecimal digits through command line?

后端 未结 5 681
暗喜
暗喜 2021-01-31 14:00

I\'d like to put together a command that will print out a string of 32 hexadecimal digits. I\'ve got a Python script that works:

python -c \'import random ; prin         


        
5条回答
  •  独厮守ぢ
    2021-01-31 14:43

    Here are a few more options, all of which have the nice property of providing an obvious and easy way to directly select the length of the output string. In all the cases below, changing the '32' to your desired string length is all you need to do.

    #works in bash and busybox, but not in ksh
    tr -dc 'A-F0-9' < /dev/urandom | head -c32
    
    #works in bash and ksh, but not in busybox
    tr -dc 'A-F0-9' < /dev/urandom | dd status=none bs=1 count=32
    
    #works in bash, ksh, AND busybox! w00t!
    tr -dc 'A-F0-9' < /dev/urandom | dd bs=1 count=32 2>/dev/null
    

    EDIT: Tested in different shells.

提交回复
热议问题