How to make a unique temporary file in Bash on Linux?
Posted on In QAIt is common to make a unique temporary file. How to make a unique temporary file in Bash on Linux safely and quickly?
You can use the mktemp
program.
In the simplest way,
tmpfile=$(mktemp)
The file will be like /tmp/tmp.j0wD39Mr3b
if you do not prefer any meaningful names.
You can set the file to your preferred string like myapp.<6 char random string>
by
tmpfile=$(mktemp -t myapp.XXXXXX)
Your temporary file will be like /tmp/myapp.V9XnA6
.
You may also choose the directory where the temporary file is in by setting the -p
option.
Check more about mktemp
in mktemp
man page.