If you are going to change properties of the shell as a whole in part of your script, consider limiting the scope of that change only to the needed part of the script to avoid unexpected effects on the rest of it. Watch for these in particular:
- The working directory
- The positional parameters ($1, $2 ... )
- Environment variables, especially PATH
- Shell-local variables, especially IFS
- Shell options with set (such as -x) or shopt (such as dotglob)
- Shell resource limits set with ulimit
We already saw one effective means of limiting the scope of variables in Chapter 5, Variables and Patterns, by applying them as prefixes to a command:
IFS=: read -r name address
This limits the scope of the IFS change to the read command only.
For other types of shell state change, we have to get a bit more creative:
- Keeping the script itself so short that...