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.)
echo -- "$KEY"would not help. It would output-- -n.-- -nprintfis always better thanechowhen you don't have complete control over what input you want to print.rcclone for Unix and its es/akanga derivatives supportecho --. Early versions ofzshdid as well, but the end-of-option delimiter later changed to-for consistency with Bourne/Korn shell and builtins I suppose.