blackhole://nilFM

posix shell tips

Tricks for increasing performance or acheiving unusual goals in POSIX shell scripts.

grep is pretty great by default but sometimes you just need to check if a string is present in a file:

if [ ! grep -Fq $string $filename ]; then ...

-F does a literal search instead of a regex search, so it will use less CPU time.

If you've got an "array" of strings separated by spaces, you can avoid forking into awk to get a field:

set -- $array
first=$1
second=$2

You will lose access to the arguments passed to your function or script though.