Making Your BASH Programs Run Faster | skybert.net

Making Your BASH Programs Run Faster


Use BASH builtins whenever possible

Forking out a sub process is expensive, therefore, try to use the builtin features whenever possible. For instance, using the builtin String manipulation features in BASH can save you many seconds (or even minutes!) on your running time of larges BASH programs.

# This is slow
file_name=$(basename "${file}")

# This is fast
file_name=${file##*/}
# This is slow
dir=$(dirname "${file}")

# This is fast
dir=${file%/*}

With just these two enhancements, I reduced the running time of one BASH program from over 8 minutes to under 4 minutes. More than twice as fast!


gmail torstein.k.johansen @ gmail ~ twitter @torsteinkrause ~