Having established the difference between functions and scripts, how can we know which is the best to use in which situation?
The choice can be subtle, but here's a basic guide to start:
- If you want your command only to be limited to an interactive shell, for example to override interactive behavior, add options to a command, or other shortcuts, use a function. Filesystem programs are useable outside the Bash shell, while functions are limited to the Bash process only.
- If you want your command to affect a running shell process, such as to change your current shell directory, or to read or set shell variables, use a function. Filesystem programs cannot change properties of your current shell process.
- In all other situations, use a script as a filesystem program in a bindir specified in $PATH. This script will then be callable by the...