Also another script to create a random four-digit hexadecimal number,
suitably modified
I would not call that modification "suitable". At least, it is not
"reasonable" to call an additional program (tr) to remove a character that
was purposefully concatenated. Just don't concatenate it in the first place:
simply write 'openssl rand -hex 2'.
To sum up (could you try to be brief and clear?): you want to replace, in an
IPv6 address, the n-th group of four hexadecimal digits (with n a random
integer from 4 to 7) with a random one. Right? You did the hardest, finding
ways to generate the random numbers. The rest is sed's classical s command
to substitute:
$ echo $ipv6_address | sed s/'[0-9a-f]\{4\}/'$(openssl rand -hex 2)/$(shuf -i
4-7 -n 1)