Skip to content

Latest commit

 

History

History
75 lines (48 loc) · 1.22 KB

File metadata and controls

75 lines (48 loc) · 1.22 KB

Good question 👍 In Bash, you can generate random values using the special variable $RANDOM.

Here’s how it works:


1. Basic $RANDOM

echo $RANDOM
  • Outputs a random number between 0 and 32767.
  • Example: 19382

2. Generate number in a range

echo $(( RANDOM % 100 ))     # 0–99
echo $(( (RANDOM % 50) + 1)) # 1–50

3. Multiple random numbers

for i in {1..5}; do
  echo $RANDOM
done

4. Random string (letters + numbers)

tr -dc 'A-Za-z0-9' </dev/urandom | head -c 12
  • Generates a 12-character random string like aK3dP9xTq7Wc.

5. Random password (with symbols)

PASSWORD=$(tr -dc 'A-Za-z0-9!@#$%^&*()_+=' </dev/urandom | head -c 16)
echo $PASSWORD
  • Example output: G7@h2Kp9!xD4LmQz
  • Good for secure auto-generated passwords.

⚡ In your user creation script, instead of:

PASSWORD=$(date +%s%N)

You can use:

PASSWORD=$(tr -dc 'A-Za-z0-9!@#$%^&*()_+=' </dev/urandom | head -c 12)

That way each new user gets a random, strong password.


Do you want me to replace your password generation line in the script with this stronger random password generator?