Linux - Sort functions in a Bash script
Learn how to use diff and sort in Bash to check if functions in your shell scripts are defined in alphabetical order. Includes a script to scan an entire directory.
This article is over a year old but was reviewed on July 30, 2026 — the content is still accurate.
This article is part of the Writing better Bash scripts series:
- Bash - ASCII art
- Bash - Loading environment variables from a file
- Bash - Script to add logging features to your script
- Bash - Echo on the console and in a logfile in the same time
- Linux - Compare two versions of the same script
- Linux - Sort functions in a Bash script
- Linux - Generate documentation from Bash scripts
- Linux - Take advantage of the number of CPUs you have; start concurrent jobs
- Linux - Using a progression bar in your script
- Linux - Bash scripts - Running unit tests with bats/bats

This article checks whether functions in a Bash script are declared in alphabetical order, by extracting function names with grep/awk and diffing the as-is list against a sorted copy using diff --side-by-side. It's then turned into an order.sh script that scans every .sh file in a folder and silently reports nothing for correctly sorted files, making misordered scripts easy to spot.
In a previous article, we've seen a simple CLI command to display the list of functions present in a script but what about a Bash script that will scan an entire folder; retrieve any .sh scripts and check if functions are sorted in the files?
Result
diff --side-by-side between the as-declared function order and the sorted order tells you
instantly whether a script's functions are alphabetized — mismatched rows point straight at
what to move:

Fix the order and rerun until every function lines up and you get a friendly confirmation:

Create a simple Bash script to play with this blog post
Please create the /tmp/bash/console.sh file on your disk with this content:
#!/usr/bin/env bash
function console::printCyan() {}
function console::askYesNo() {}
function console::printRed() {}
function console::printError() {}
function console::banner() {}
function console::printGreen() {}
function console::printBlue() {}This is a bash script that defines a library of console utility functions for formatted text output and user interaction. The functions are stubbed out (empty) and appear to be intended for printing colored text (cyan, red, green, blue), displaying errors, showing banners, and prompting yes/no questions to users.
As we can see, we'll just create empty functions in no particular order.
Get the list of functions in a Bash script
To get the list of functions declared in a script, just run the command below:

Ok, now, we know how to sort the list of functions in the console.
Let's use diff to compare our existing file (left-hand side) and the sorted list of functions (right-hand side) — this is what produced the comparison shown at the top of this article:
(
FILE=/tmp/bash/console.sh
printf "\e[33;1m%-39s %s\e[0;1m\n" "Left side: AS IS" "Right side: Using correct sorter"
diff --side-by-side --width 83 \
<(grep -P "^(function\s+.*)\(\)" "${FILE}" | awk '{print $2}') \
<(grep -P "^(function\s+.*)\(\)" "${FILE}" | awk '{print $2}' | sort)
)
As we can see on the image shown above, the column at the left show that the first-defined function in the /tmp/bash/console.sh script is console::printCyan followed by console::askYesNo and console::printRed for the third function declared (in that order) in the Bash script.
In the right column, we can see that the first, sorted, function is console::askYesNo, the second is console::banner and so on.
Back to the left column: the name displayed in white are already in the correct order!
Let's update partially the /tmp/bash/console.sh file and reorder some functions:
#!/usr/bin/env bash
function console::askYesNo() {}
function console::banner() {}
function console::printBlue() {}
function console::printCyan() {}
function console::printRed() {}
function console::printError() {}
function console::printGreen() {}This is a bash script that defines a library of console output functions for printing colored text and handling user interaction. The functions are named using a namespace convention (console::) to organize them logically, though they're all currently empty stubs waiting to be implemented.
Now, rerunning the same command:

We just need to put console::printRed() at the end and we'll be fine and, to give some positive feedback, use this enhanced version:
(
FILE=/tmp/bash/console.sh
printf "\e[33;1m%-39s %s\e[0;1m\n" "Left side: AS IS" "Right side: Using correct sorter"
diff --side-by-side --width 83 \
<(grep -P "^(function\s+.*)\(\)" "${FILE}" | awk '{print $2}') \
<(grep -P "^(function\s+.*)\(\)" "${FILE}" | awk '{print $2}' | sort) && printf "\n%s\n" "🎉 🎊 🕺 💃 👏 CONGRATULATIONS"
)
If you see the confirmation message shown at the top of this article, perfect, functions are correctly ordered in your script.
Process all scripts from a specific folder
We have just seen, on the command line, how to check whether a Bash script declaring functions does so in alphabetical order.
Let's move on to the industrialization of this concept: a script that will scan each .sh file in a specific folder and check whether the functions are defined in the file in alphabetical order.
A sorted script is also a script that documents itself better: Linux - Generate documentation from Bash scripts generates one Markdown page per script from the doc block of each function, in declaration order.
If this is the case, we won't have any display so as not to pollute our console.
If this is not the case, the left-hand side of the screen shows the current order of function declarations and the right-hand side shows the expected order, sorted alphabetically.
To do this, create the order.sh script on your hard drive with this content:
#!/usr/bin/env bash
clear
if [ ! $# -eq 1 ]; then echo "Usage: $0 <folder>" exit 1fi
sourceFolder="$1"
[[ ! -d "${sourceFolder}" ]] && echo "Error: Source folder ${sourceFolder} not found." && exit 1
printf "\e[37;1m%s\e[0;1m\n\n" "Check if functions declared in Bash .sh script in folder ${sourceFolder} are correctly ordered in the file."
printf "\e[33;1m%-39s %s\e[0;1m\n\n" "Left side: AS IS" "Right side: Using correct sorter"
pushd "${sourceFolder}" >/dev/null
set +e
for bashScript in *.sh; do if ! result="$(diff --side-by-side --width 83 \ <(grep -P "^(function\s+.*)\(\)" "${bashScript}" | awk '{print $2}') \ <(grep -P "^(function\s+.*)\(\)" "${bashScript}" | awk '{print $2}' | sort))"; then printf "\e[33;1m%s\e[0;1m\n" "The file ${bashScript} isn't correctly ordered" printf "\e[37;1m%s\e[0;1m\n" "${result}" fidone
set -e
popd >/dev/nullThis script checks whether functions in Bash files within a given folder are declared in alphabetical order. It compares each file's actual function declaration order against a sorted version and displays any mismatches side-by-side so developers can see which files need their functions reordered.
And now, start the script like this: ./order.sh ~/helpers. The expected parameter is the name of a folder containing .sh files.

What does that mean? My ~/helpers/api.sh script is actually (left side) really poorly sorted since there are a lot of differences with the right column (perfect ordering).
The first function in my file is api::__injectLogsToApplicationLog while there is an api::__assertHttpMethod function later in the code.
By editing my file and moving api::__assertHttpMethod at the top of my script, now, by rerunning the script:

Ok, so now, still at the right side, we can see api::__debugCurlStatement is expected in the second position, api::__doSomeCleaning as the third one then api::__executeCall() and so on (as we can see in the right side).
As soon as the api.sh file is correctly sorted, rerunning the script won't mention anymore the script but other ones.

The final objective is thus: we expect no output with the script. If all Bash scripts are correctly sorted then the order.sh script is no longer going to find any differences, which is in fact what we want.
This article is part of the Writing better Bash scripts series:
- Bash - ASCII art
- Bash - Loading environment variables from a file
- Bash - Script to add logging features to your script
- Bash - Echo on the console and in a logfile in the same time
- Linux - Compare two versions of the same script
- Linux - Sort functions in a Bash script
- Linux - Generate documentation from Bash scripts
- Linux - Take advantage of the number of CPUs you have; start concurrent jobs
- Linux - Using a progression bar in your script
- Linux - Bash scripts - Running unit tests with bats/bats





