0

I have variable in my bash script

symbols="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~#$&_+-=/\\"

How to get random from this line in range 10-15?

For example in python I can

password = ''.join(random.sample(symbols, random.randint(10, 15)))

In output

# YeHvTBX4qVrzK9
# NYcd-HR0wVvE5Cg6
# mlS=uMPieqR

In bash how can i do that?

0

1 Answer 1

0

Use the $RANDOM special variable and parameter expansion to extract symbols from the string.

#! /bin/bash

symbols='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~#$&_+-=/\'  # Stupid SO: '
count_symbols=${#symbols}
(( length = RANDOM % 6 + 10 ))
password=""
for i in $(seq 1 $length) ; do
    password+=${symbols:RANDOM % count_symbols:1}
done
echo "$password"
Sign up to request clarification or add additional context in comments.

Comments