Creating a sub-shell involves a fork()
, so it definitely has overhead compared with a local variable. While sub-shells are cheap — you don't worry about their cost when you need one — they are not free.
If your script is going to be heavily used and performance really matters (so you'll have hundreds of users all running it at the same time, many times a day), then you might worry about the performance cost of the sub-shell. OTOH, if you run it once a month and the script as a whole runs for under 10 seconds, you probably wouldn't.
However, in terms of clarity, it is much better to be explicit and declare the variables — it reduces the risk of the script breaking because someone comes along and says "this sub-shell clearly isn't needed" (and it really isn't; I'd want to remove the sub-shells from your functions).
Look at the evolution of Perl scripts. They started off as a free-for-all with variables coming into existence on demand. They have gradually become more rigorous, with normal style now being to predeclare all variables. To some extent, shells have followed a similar path — but not as rigorously as Perl. Awk is also an interesting case study; its functions use global variables unless they are arguments to the function, which leads to functions being written with 3 active arguments (say) and 5 inactive arguments that effectively define local variables. It is slightly eccentric, though it 'works'.
time
command over a 1000 tests and find out the overhead, I think it's small to non existent. – Isfahan