question

JamesPansarasa-1260 avatar image
0 Votes"
JamesPansarasa-1260 asked bhargaviannadevara-msft edited

Problem generating random string in Azure Cloud Shell Bash

I am trying to generate a random string of a certain length in Bash. This works in every other Bash shell I could test (Ubuntu 18.04 LTS and Debian 10)

 $ cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 10 | head -n 1
 n8a4wxvb01
 $

cat /dev/urandom – endless stream of bytes

tr -dc 'a-z0-9' – only return lowercase characters and digits

fold -w 10 – break at 10 characters

head -n 1 – return the first line of the stream

In the Azure Cloud Shell Bash, this hangs. It returns the random string but never exits. The call to head -n 1 never exits after printing the value.

I have a work around but it seems backwards

 head -n 10 /dev/urandom | tr -dc 'a-z0-9' | fold -w 10 | head -n 1
azure-cloud-services
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

bhargaviannadevara-msft avatar image
0 Votes"
bhargaviannadevara-msft answered bhargaviannadevara-msft edited

Hello @JamesPansarasa-1260,

IMHO, this issue is not specific to Cloud Shell as such, and can happen with a *nix terminal.

As you already found, this command should work on your machine and the Cloud Shell:

head /dev/urandom | tr -dc a-z0-9 | fold -w 10 | head -n 1

Here are some possible explanations:

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.