I have a bash function which is invoked as a child process (though backticks) from by bash script. Is there any way to have this function (running in the child process) modify the parent's trap handlers? This function creates a temporary file which I would like to be cleaned up when the parent exits. As an example
function get_roe_dir() {
tmp_dir=`mktemp -d`
if [ $? -eq 0 ]; then
# some processing on $tmp_dir
echo "$tmp_dir"
# Add "rm -rf $tmp_dir" to parent's EXIT trap
fi
}
Then, in the calling script, I have something like:
roe_dir=`get_roe_dir`
# Some processing using files in $roe_dir.
Once this script exits, I'd like $roe_dir
to get deleted (using the EXIT trap). Any ideas for clean ways to achieve this?
I cannot add an EXIT trap to the get_roe_dir()
function because it is executed in a sub-shell to capture the output. Therefore, as soon as get_roe_dir()
has returned, the subshell exits and the EXIT trap is called, deleting the temporary directory it created.