DEV Community

John Mitchell
John Mitchell

Posted on

I despise bash but...

... use it constantly. It's just so useful.

Two tips:

1) first line of ALL SCRIPTS is:

set -euo pipefail # strict mode 
Enter fullscreen mode Exit fullscreen mode

This makes the script crash so you can fix it if any command gets an error, or a segment of a pipe gets an error. It'll also crash if a variable gets used before being set.

A program that does the wrong thing then silently continues, is a bad bad program.

2) rewrite the script in a real language (Python?) if it has more than 3 conditionals or loops.

Peronally I find conditionals to be do-able in Bash, but loops tend to be problematic.

I've written thousands of lines of Perl and Awk and other things in my day, but Bash and Python cover 100% of my work these days.

BONUS:

3) set -o xtrace also known as set +x is also great.

Print each command before it's executed, making code run really obvious. We love obvious.

Top comments (0)