1

So I am TRYING to make a bash file that rotates my MAC address every 10 minutes with a random hexadecimal number assigned each time. I would like to have a variable called random_hexa assigned to the result of this command: openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'. I would then take the variable and use it later on in the script.

Any Idea how to take the result of the openssl command and assign it to a variable for later use?

Thanks!

Ryan
  • 43
  • 5
  • 11

3 Answers3

4

Store the variable like so:

myVar=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')  

Now $myVar can be used to refer to your number:

echo $myVar

$() runs the command inside the parenthesis in a subshell, which is then stored in the variable myVar. This is called command substitution.

Seth
  • 498
  • 3
  • 16
  • 31
1

You want "command substitution". The traditional syntax is

my_new_mac=`openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//'`

Bash also supports this syntax:

my_new_mac=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')
John Bollinger
  • 138,022
  • 8
  • 74
  • 138
0

You can store the result of any command using the $() syntax like

random_hexa=$(openssl...)

Eric Renouf
  • 13,300
  • 3
  • 41
  • 62