r/linux_programming • u/fazi_d • Jun 15 '23
When exactly EXIT is sent?
I fought - at script exit but:
#!/bin/bash
make_temp() {
t=$(mktemp)
trap "rm $t" EXIT
echo $t
}
tt=$(make_temp)
ls $ttIt gives me 'no such file or directory'
How to catch script exit but not function exit?
0
Upvotes
View all comments
Show parent comment
1
u/aioeu Jun 15 '23 edited Jun 15 '23
That's right.
When writing Bash scripts there are some tricks you can use to avoid subshells (e.g. the so-called "upvar" technique for assigning variables in parent stack frames). This is good idea generally: subshells aren't particularly cheap to set up and tear down, and it's a lot easier to reason about the state of your shell when you've only got one of them.