0

I want to create a custom variable or command that can be called on like $RANDOM, but for strings. I have a script that creates a random string, using /dev/urandom . The script produces a randomly generated string each time the script is run.

#!/bin/bash

rando=$(head -100 /dev/urandom | tr -dc a-zA-Z0-9 | fold -w 15 | head -1)

echo $rando

If I create an alias or environmental variable that calls on the script, it will produce only one variation of a random string until a new bash sessions is created. How can I make it so it will create a new variation of the random string in the same bash session?

5
  • 1
    Wrap it up in a function in your .bashrc. Then every time you call it, the code will run. Commented Nov 25, 2019 at 19:13
  • Huh? The variable $rando is of course the same until you change it, but calling dd etc again will create a new value each time. Or are you asking how to make a function around this code? Commented Nov 25, 2019 at 19:15
  • RANDOM is a special built-in parameter; you can't define your own variable whose value changes every time you expand it. Commented Nov 25, 2019 at 20:05
  • This might work, too: tr -dc a-zA-Z0-9 </dev/urandom | head -c 15 Commented Nov 25, 2019 at 20:08
  • 1
    Worked like a charm @JNevill . This accomplished what I wanted to do. Thank you Commented Nov 25, 2019 at 20:23

1 Answer 1

2

As replied by JNevill, you can place it as a function in your bash profile :

function RANDOM(){
    rando=$( head -100 /dev/urandom | tr -dc a-zA-Z0-9 | fold -w ${1:-15} | head -1 )
    echo $rando
}

RANDOM
RANDOM 4
RANDOM 40

Output

17oxDRkl2O1c9iz
vfgZ
4xlVNyINrBj8XT04nkQWIVOTHAV51eVxtVNEyRW0

I added an optional first argument so you can control the length of the string as well.

Sign up to request clarification or add additional context in comments.

4 Comments

This is exactly what I was looking to accomplish, thank you! So this works because it calls on the function each time to create the string, rather than a variable being declared by the script when bash starts up, right? Why does an alias that calls on the script not work then?
Im not sure of your actual implementation. But for what you describe, you seem to be confusing environment variables with aliases. if you do this : export RANDOM=$(SOME COMMAND CALL HERE) the value of RANDOM will always be the same. While if you create an alias you always get an actual command being executed. Anyway, I almost always end up using functions instead of aliases. Code can be more complex and easier to read. So if in doubt, I would recommend to default to functions most of the time and avoid aliasing.
Starting to use functions more in my scripts, as they do make everything look a lot cleaner. Thank you for your insight, marked your answer as the solution. :)
I'd recommend simplifying this by removing the rando variable and echo -- just have the pipeline produce output directly.

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.