2

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
  • 53
  • 5
  • 11

3 Answers3

6

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
  • 528
  • 3
  • 16
  • 32
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
  • 160,171
  • 8
  • 81
  • 157
0

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

random_hexa=$(openssl...)

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67