Is there a method/command to check for the dependencies of a bash script? In other words, a response to this question : Which libraries should a user install to run the script?

I can do this manually by reading the script and check what other libraries/commands it calls but this not evident for long scripts.


The incredibly arcane autoconf system has been used to

produce shell scripts to automatically configure software source code packages. These scripts can adapt the packages to many kinds of UNIX-like systems without manual user intervention. Autoconf creates a configuration script for a package from a template file that lists the operating system features that the package can use, in the form of M4 macro calls.

If that is not sufficiently scary, wait until you try to write your first autoconf script. The authors of autoconf are not shy about its difficulty:

Those who do not understand Autoconf are condemned to reinvent it, poorly. The primary goal of Autoconf is making the user's life easier; making the maintainer's life easier is only a secondary goal. Put another way, the primary goal is not to make the generation of configure automatic for package maintainers; rather, the goal is to make configure painless, portable, and predictable for the end user of each autoconfiscated package. And to this degree, Autoconf is highly successful at its goal — most complaints to the Autoconf list are about difficulties in writing Autoconf input, and not in the behavior of the resulting configure.

On the other hand, you will come out of the process knowing more about the fiddly bits of various systems than you'd ever imagined existed.

These days, personally, I would assume a much more standard Debian-ish distribution was my target and not make myself write another autoconf script. I am lucky to have the luxury to choose that; you may not have such latitude.


This is not a simple task to automate, because a script may use constructs that defeat static analysis. If it ever uses eval or any prefix like time or nice, it won't be as simple as running something like egrep -o '^[^ ]+ ? ' to get commands and running them through which or type.

In the end, the only way to be absolutely sure is to run the script and find out what fails. If a script is well-written, it will check for non-standard commands before running. If not, trial-and-error is the only way to be certain.

Having said that, something like this could help:

egrep -o -e '^[^ ]+ ? ' -e '[a-zA-Z0-9]+' "$1" | sort -u | {
    while read line
        if type $line &>/dev/null
            echo "$line found"
            echo "Error: $line not found"
} | sort

The output will look like:

$ ./check i_wonder.sh
cd found
echo found
elif found
else found
Error: abort not found
Error: checkurl not found
Error: cleanup not found
Error: count not found
Error: debug not found
Error: deleteFile not found
Error: die not found
find found
for found
grep found
if found
mv found
readarray found
rm found
shopt found
size found
sleep found
stat found
trap found
unset found
while found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.