Using "$Random" to Generate a Random String in Bash

How to generate a random string in bash?

This might be what you're looking for:

#!/bin/bash

chars='abcdefghijklmnopqrstuvwxyz'
n=10

str=
for ((i = 0; i < n; ++i)); do
str+=${chars:RANDOM%${#chars}:1}
# alternatively, str=$str${chars:RANDOM%${#chars}:1} also possible
done

echo "$str"

Your code was almost correct, except that you've missed the += operator. The += operator appends to the variable’s (str here) previous value. This operator was introduced into bash with version 3.1. As an aside note, it can also be applied to an array variable in order to append new elements to the array. It also must be noted that, in an arithmetic context, the expression on the right side of the += operator is evaluated as an arithmetic expression and added to the variable’s current value.

How to generate a random string begining with lowercase letters and ending with lowercase letters?

If you have a command

$(SOMETHING)

bash first executes SOMETHING as command, then collects its standard output, and replaces the original $(....) part with this standard output, which, since it is the only thing on the line, means that this standard output as a new command and executes that one too. For instance, the line

$(echo ls)

is just a ridiculously cumbersome way to execute a simple ls. In your case, you don't want the generated random string to be executed, but - for instance - being stored in a variable. Variable assignments in bash look like

VARNAME=STRING

Hence, if you do a

my_beautiful_random_string=$(tr -dc a-z </dev/urandom|head -c 1)$(tr -dc a-zA-Z </dev/urandom|head -c 6)$(tr -dc a-z </dev/urandom|head -c 1)

you would get your desired pattern on the variable my_beautiful_random_string. This would work, but the solution is a bit inefficient, because you create 6 child processes and 3 pipes fpr generating one string. If you worry about efficiency (for instance, because you are doing this in a loop), here are two alternatives:

You could (using your approach) create a single string of 7 mixed case characters and then convert the first and last one to lower case (see the paragraph on 'Case modification' in the bash man page). This would cost you only 2 child processes.

Alternatively, you could get rid of /dev/random and use the bash variable RANDOM. In this case you would first define a bash array holding the 26 characters of the alphabet, then for generating a single random character, calculate a random integer in this range (using $RANDOM) and pick one element of this array. Repeat it 7 times and you have your string. If you do this properly, this does not need any child process at all, but the code is more complex.

Using $RANDOM to generate a random string in Bash

Use parameter expansion. ${#chars} is the number of possible characters, % is the modulo operator. ${chars:offset:length} selects the character(s) at position offset, i.e. 0 - length($chars) in our case.

chars=abcd1234ABCD
for i in {1..8} ; do
echo -n "${chars:RANDOM%${#chars}:1}"
done
echo

Generating random number between 1 and 10 in Bash Shell Script


$(( ( RANDOM % 10 )  + 1 ))

EDIT. Changed brackets into parenthesis according to the comment.
http://web.archive.org/web/20150206070451/http://islandlinux.org/howto/generate-random-numbers-bash-scripting

How to generate a random string in a specific range (bash)

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"

BASH - Problem with generating a different random string in each curl command


the goal is to generate a different and random string for each curl command.

So just put the string generation in the loop........

#!/bin/bash
for i in {1..2}; do
newuuid=$(cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1)
curl "https://mywebsite.com/comment... ...&text=$newuuid"
sleep 60
done

if it's generating UUID, call uuidgen if available to generate uuid.

cat /dev/urandom | tr -dc "a-zA-Z0-9" | fold -w 5 | head -n 1 is heavy execution wise and may deplete entropy input very fast.

How to generate 10 million random strings in bash

This will not guarantee uniquness, but gives you 10 million random lines in a file. Not too fast, but ran in under 30 sec on my machine:

cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 10000000 > file

LINUX SHELL SCRIPT Assign random string as password

Well, yeah ... that would be a correct message; you're not assigning the string.

password=$( date +%s%N | sha256sum | head -c 10 )


Related Topics



Leave a reply



Submit