Hello!
I try to write value in environment variable from my script, but somthing I don’t see changes, but I see as script write value to log. What is wrong I do?
How do you want to use the generated key?
You say it is a secret key, but looks like anyone can see it…
You could simply store the key into a file and use it from there.
Something like this:
# file for storing the key
touch /home/your-account/.secret-key # change your-account to your username
# create and store the key
openssl rand -hex 32 > /home/your-account/.secret-key
# use the key somewhere
keyval=$(cat /home/your-account/.secret-key)
I Thinked about this solution but not apply. So if environment variables is limited. Then your answer is a best way, because file can limit to write end edit. Thank you!
That’s a useless use of cat – you’re launching a separate program from your script just to get the contents of a file into a variable. In fact, there is almost never any need to use cat in a Bash script, as Bash provides facilities for that.
Don’t do that… Do this instead:
keyval=$(<"$HOME/.secret-key")
Indeed. The OP might want to consider the security implications of that. Depending on the use case, it may or may not be a good idea.