Temporary files are a security problem only if one creates them in a shared directory such as /tmp
or /var/tmp
. To create temp file in a safe and secure way, just place it in a directory, like the user's home directory, where other users do not have write permission. For example:
tmpname=$HOME/.deleteme$$
some_command >"$tmpname"
By using a non-shared directory, all the issues of race conditions go away. (If an attacker already has write access to your home directory, then you are pwned and the attacker has no need to play with temp files and race conditions.)
To make sure that the temp file is deleted, create a trap
trap 'rm -f "$tmpname"' EXIT
The trap statement should be placed in the script before the temp file is created.