Scripting Tips#

Best practices and useful tools for shell scripting.

set -e#

Add set -e at the beginning of your script to make it exit immediately if any command fails. This prevents error propagation.

#!/bin/bash
set -e

if with grep#

Since grep returns an exit code of 0 on success and 1 on failure, it can be used directly in an if condition. Use -q, --quiet to suppress output.

if grep -q "pattern" <file>; then
    echo "Pattern found"
fi

Other Useful Tools#