I am using bash and most interested in an answer for bash, but you can answer about how to do it in other shells if you want.
I have an environment variable, KEY, and I want to store the value of that environment variable in a a file. Currently I am doing
echo "${KEY}" > ./key.pem
In practice this seems to be OK, but in theory this breaks when KEY=-n.
$ export KEY=-n
$ echo BEGIN "${KEY}" END
BEGIN -n END
$ echo "${KEY}" END
END$
So, is there a better way to store the value of a single environment variable in a file? (Note that export and declare will include the name of the variable in their outputs, which is no good for me.)