1

I want to generate strings with lengths between 1-4 with my bash script. I know how to create strings with set length, but want to know if there is a slight alteration to the following command to make it choose between 1-4 length.

cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 4

This will create strings with numbers/letters with standard length 4. Any way to tweek it to fit my needs?

3 Answers 3

2

Not the smartest solution but this should work I guess

cat /dev/urandom | tr -dc A-Za-z0-9 | head -c "$(shuf -i 1-4 -n 1)"
Sign up to request clarification or add additional context in comments.

2 Comments

Should work, but because my program already takes about 10 seconds for what I want it to do, I was afraid that adding another random function here might increase the execution time further than I want it. Was really hoping there was a fix for it that I wasn't aware of, based on altering the "head.." part.
Useless cat. Also use character class and switch locale for consistent results across systems: LC_ALL=POSIX tr -dc '[:alnum:]' </dev/urandom | head -c "$(shuf -i 1-4 -n 1)"
2

That's simple: Use a random number/character generator to generate the argument to head -c too.

In bash you can use the built-in variable $RANDOM together with an arithmetic expression. The random numbers are not that good (especially when using % someBigNumber) but sufficient for most scenarios:

< /dev/urandom tr -dc A-Za-z0-9 | head -c "$((RANDOM % 4 + 1))"

If you need better randomness or a portable script use /dev/urandom again:

< /dev/urandom tr -dc A-Za-z0-9 | head -c "$(< /dev/urandom tr -dc 1-4 | head -c 1)"

Comments

0

Here a method to generate n random strings of random min length to max length:

It reads a stream of random [:alnum:] alphanumeric characters; each time with a different random amount, and store it in an array.

This method maximizes efficiency with:

  • The use of Bash's built-in functions.
  • A single call to external command tr.
  • Reading random unsigned bytes from the /dev/urandom fast random generator device.
#!/usr/bin/env bash

# count: Number of strings to generate
# l_min: Minimum length of string
# l_max: Maximum length of string

declare -i count=10 l_min=4 l_max=8

# Array containing random strings

declare -a rand_strings=()

declare -i i=0
while [ $i -lt $count ]; do
  declare -i l=$((RANDOM%(l_max-l_min+1)+l_min))
  read -r -N $l rand_strings[i]
  i=$((i+1))
done < <(
  LC_ALL=POSIX
  tr -dc '[:alnum:]' </dev/urandom
)

printf '%d ranndom strings of %d to %d characters\n' $count $l_min $l_max
printf '%q\n' "${rand_strings[@]}"

Sample output:

10 ranndom strings of 4 to 8 characters
4QeeD9oT
GZQ7dD0
r3wb5D
17HBZhp
SP1i
t1OHq
1L4qpz7T
F5zG
Dw9HQ
ysG2chf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.