Match complete literal records with a grep allowlist
Names such as app.prod contain characters with regular-expression meaning. For a plain text allowlist, GNU grep can treat each pattern literally and require it to match the entire record.
Run the example
Tested on Linux with GNU Bash 5.2.37, coreutils 9.7, grep 3.11 and diffutils 3.10. Use one Bash session and a text editor. Create a disposable working directory before saving the source:
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Save as demo_linux_grep_exact_literal_allowlist.sh:
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -f -- "$fixture_dir/allow.txt" "$fixture_dir/records.txt"; rmdir -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
printf '%s\n' 'app.prod' 'worker[1]' > allow.txt
printf '%s\n' 'app.prod' 'appXprod' 'prefix-app.prod' 'worker[1]' > records.txt
grep -F -x -f allow.txt -- records.txt
if grep -F -x -e 'absent' -- records.txt; then
exit 1
else
status=$?
test "$status" -eq 1
printf 'Absent record: no match\n'
fiRun it:
bash demo_linux_grep_exact_literal_allowlist.shObserved output:
app.prod
worker[1]
Absent record: no match-F disables regular-expression interpretation, -x requires a full-line match, and -f reads one pattern per line. The output retains the input record order. The second search deliberately finds nothing and handles status 1 without hiding an execution error.
Limits and input policy
This is line-oriented matching, not CSV parsing or a validation system for arbitrary binary data. A blank pattern line matches an empty record with -x; an empty pattern file matches nothing. Decide whether blank entries and carriage returns are valid before generating the allowlist. GNU grep returns status 2 for an error, so a script must distinguish that from no matches.
Clean up
Remove the saved script and the empty working directory in the same Bash session:
rm -- demo_linux_grep_exact_literal_allowlist.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirIf the directory is not empty, inspect its contents before removing anything else.
Reference
Acknowledgements
This post was created with assistance from AI.