Temporary folder to a setup script
I'm creating a bash script which will download, run and remove a bash script.
I'm thinking to use the /tmp
Which temporary folder could I use to it?
The easiest to create a unique temporary folder is to use mktemp
:
my_tmpdir=$(mktemp -d)
This will generate a unique name (e.g. /var/folders/8b/mn2vgjs42gs83krfy1fwxwb80000gp/T/tmp.LYnaqveK
), create the directory with that path automatically and assign the path to my_tmpdir
.
What I usually do within shell scripts or functions is
function do_stuff() {
local t=$(mktemp -d)
trap "rm -rf $t" RETURN
(
cd $t
## do stuff
)
}
This will run the whole script in a temporary directory, it will also make sure that the directory is removed again afterwards.