DEV Community

Cover image for Bash your stash - optimize
Vladi Rahmanov for Target-Ops

Posted on

Bash your stash - optimize

Optimizing Your Bash Scripts: Best Practices and Code Samples

Bash scripting is a powerful tool for automating tasks in the Linux environment. However, as scripts grow in complexity, they can become slow, inefficient, and harder to maintain. Here are key techniques to optimize your Bash scripts for better performance, readability, and reliability.

1. Use Built-in Commands Over External Commands

Built-in commands like [[, let, and declare are much faster than external commands like grep, sed, and awk.

❌ Instead of using external commands:

if [ $(echo "Hello" | grep -c "H") -eq 1 ]; then
    echo "Found"
fi
Enter fullscreen mode Exit fullscreen mode

✅ Use built-in pattern matching:

if [[ "Hello" == *H* ]]; then
    echo "Found"
fi
Enter fullscreen mode Exit fullscreen mode

2. Avoid Useless Use of cat

Many scripts use cat unnecessarily. You can often pass files directly to commands.

❌ Don't use:

cat file.txt | grep "pattern"
Enter fullscreen mode Exit fullscreen mode

✅ Instead use:

grep "pattern" file.txt
Enter fullscreen mode Exit fullscreen mode

3. Use Arrays Instead of Loops When Possible

Arrays in Bash can help reduce code complexity, especially when dealing with multiple values.

❌ Simple loop:

for item in item1 item2 item3; do
    echo $item
done
Enter fullscreen mode Exit fullscreen mode

✅ Array-based approach:

items=("item1" "item2" "item3")
for item in "${items[@]}"; do
    echo $item
done
Enter fullscreen mode Exit fullscreen mode

4. Use String Manipulation Instead of Subprocesses

Bash's built-in string manipulation features are faster than external commands.

❌ Using external commands:

output=$(echo "Hello World" | sed 's/World/Bash/')
Enter fullscreen mode Exit fullscreen mode

✅ Using built-in string substitution:

output="Hello World"
output=${output/World/Bash}
Enter fullscreen mode Exit fullscreen mode

5. Minimize the Use of Subshells

Avoid subshells when possible as they create child processes.

❌ Using subshell:

files=$(ls *.txt)
for file in $files; do
    echo $file
done
Enter fullscreen mode Exit fullscreen mode

✅ Direct approach:

for file in *.txt; do
    echo $file
done
Enter fullscreen mode Exit fullscreen mode

6. Use [[ for Conditional Tests

The [[ operator is more efficient and safer than the traditional [ ] operator.

❌ Old style:

if [ "$var" == "value" ]; then
    echo "Equal"
fi
Enter fullscreen mode Exit fullscreen mode

✅ Better approach:

if [[ "$var" == "value" ]]; then
    echo "Equal"
fi
Enter fullscreen mode Exit fullscreen mode

7. Enable Nounset and Exit on Error

Make scripts more robust with error handling:

set -u  # Treat unset variables as an error
set -e  # Exit on any error

function my_function() {
    echo "This is my function"
}
my_function
Enter fullscreen mode Exit fullscreen mode

8. Process Multiple Files in Parallel

Use xargs to parallelize operations for better performance.

❌ Sequential processing:

for file in *.txt; do
    process_file "$file"
done
Enter fullscreen mode Exit fullscreen mode

✅ Parallel processing:

ls *.txt | xargs -n 1 -P 4 process_file
Enter fullscreen mode Exit fullscreen mode

9. Use declare for Local Variables

Keep variables local to prevent scope pollution:

my_function() {
    declare local_var="some value"
    echo "$local_var"
}
Enter fullscreen mode Exit fullscreen mode

10. Avoid Forking Processes Unnecessarily

Use built-in Bash functionality instead of external commands for basic operations.

❌ Using external command:

result=$(echo "$num1 + $num2" | bc)
Enter fullscreen mode Exit fullscreen mode

✅ Using Bash arithmetic:

result=$((num1 + num2))
Enter fullscreen mode Exit fullscreen mode

Conclusion

These optimization techniques can significantly improve your Bash scripts' performance and reliability. Remember to:

  • Prefer built-in commands
  • Avoid unnecessary processes
  • Use proper variable scoping
  • Leverage parallel processing when possible
  • Implement proper error handling

The key to writing better Bash scripts is consistent practice and attention to these optimization details.

Top comments (0)