In software projects, often would appear a 'scripts' directory with various scripting utilities. The unexperienced bash script writer would usually litter up the directory with many execution entry points such as:

If we would like to clean this up and combines these functions to a single file, say, how one would do so?

A nice way to accomplish this is the following:


# ... common code here ...

build() {
    # Original content and logic of

run() {
    # Original content and logic of

deploy() {
    # Original content and logic of


(for clarification regarding "$@", see the previous post regarding execution relay)

I call this method the Functional Command Relay pattern, as it allows execution of each function with the parameters to these functions relayed as-is.

$ ./ build <params>
<output of the original build logic>

We can choose how strict we are with the first parameter. For example, by replacing the last "$@" line with the following:

case $1 in
    build|run|deploy) "$@" ;;
    '') print_help; exit -1 ;;
    *) echo "Invalid command '$1'" ; exit -1 ;;

Worth to mention that the under-utilized function concept in bash is more powerful that it may seem at first.

The functions behave like mini-scripts in the same manner that original scripts would behave, and similar to "real" executable programs that are already reachable from $PATH. For instance, it is possible to use echo for output and have use functions as components in shell pipes.