how to optimize bash script

tags: learning programming

content

  • bash itself isn’t slow, it’s how you use it
    • Bash is not slow at running commands. It’s slow at being a programming language.

avoid huge loops

  • below code spawns an unknown number of processes
    • if there’re 1000 files, 1000 processes will be spawned
for file in *; do 
	wc -l "$file"
done 
  • do this intead:
wc -l *

use other process for heavy lifting

  • Use awk, sed, grep, find for heavy operations.

command usage

  • Certain command usages can be streamlined to enhance performance. For example, instead of piping echo to another command, use direct file writes or built-in print options:
# Less efficient
echo "Some text" | grep "text”
 
# More efficient
grep "text" <<< "Some text"
  • Avoid Using cat for Repeated I/O Work
# Less efficient
cat largefile.txt | while read line; do
    echo "Processing $line"
done
 
# More efficient
while read line; do
    echo "Processing $line"
done < largefile.txt

up

down

reference