Choosing between functions and scripts

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. ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.