Sample Header Ad - 728x90

Putting the contents of /dev/urandom into a variable?

1 vote
3 answers
1830 views
This is a follow on from my previous thread :- https://unix.stackexchange.com/questions/651412/passing-date-command-into-a-variable Ok so effectively I would like /dev/urandom to generate 4 digits for me. And place them into a variable. Here is an example of my bash script :-
#!/bin/bash
for (( c=0; c<=10; c++))
do
        a="$(tr -dc '[:digit:]' < /dev/urandom | fold -w 4)"

        echo $a
done
The only reason I wish to "Echo" the results is so I can actually see that /dev/urandom has generated 4 digits for me correctly. For my bash script I wish to make, I need to do this programmatically as I wish to do further operation's on the generated results. So I need not "Echo" the results. so is a="$(tr -dc '[:digit:]' < /dev/urandom | fold -w 4)" Actually placing 4 random digits into the variable "a"? when I Echo "a" from my script I just get a hanging console and have to use "CTRL+C" to bring my console back. All I am trying do is place 4 random digits from /dev/urandom into a variable. I m a new to bash scripting so all advice welcome and thank you in advanced for any help or suggestions.
Asked by Shanzem (35 rep)
Jun 22, 2021, 09:02 AM
Last activity: Jul 16, 2025, 02:32 AM