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!

3 Answers 3

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.

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

Comments

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/.$//') 

Comments

0

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

random_hexa=$(openssl...)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.