GREP_OPTIONS Using --line-buffered

Problem

Grep out of the box has issue with commands like tail -f when we want to pipe after we have grepped.

The issue is demonstrated by the following:

#!/bin/bash

# Generate output every second
generate_output() {
    for i in {1..10}; do
        echo "Line $i: This is a test"
        sleep 1
    done
}

# Non-flushing grep example
non_flushing_grep() {
    generate_output | grep "test" | while read line; do
        echo "Processed: $line"
    done
}

# Run the non-flushing grep function
non_flushing_grep

If you run this you will see NO OUTPUT until all 10 lines finish. Which is not what we would like when we are using commands like tail -f to tail the logs.

Solution

You can specify GREP_OPTIONS to change default behavior of grep adding --line-buffered

export GREP_OPTIONS='--line-buffered'

Now the same script above will output lines as they are generated, rather than waiting for all the lines to process.

Side note

You can also make grep print in color as default using GREP_OPTIONS

export GREP_OPTIONS='--color=auto --line-buffered'

Backlinks