Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Last active September 2, 2017 15:36
Show Gist options
  • Save aleclarson/99c8eaa1de542c530f9af7112cb603fb to your computer and use it in GitHub Desktop.
Save aleclarson/99c8eaa1de542c530f9af7112cb603fb to your computer and use it in GitHub Desktop.
Read a .gitignore-like file with Bash
#!/bin/bash
read_lines() {
while IFS='' read -r line || [[ -n $line ]]; do
if [ -z "${line// }" ] || [[ $line == "#"* ]]; then continue; fi
echo "$line"
done < "$1"
}
if [ -f "$1" ]; then read_lines "$@"; fi
@aleclarson
Copy link
Author

  • IFS='' prevents leading/trailing whitespace from being trimmed
  • [[ -n $line ]] prevents the last line from being ignored if it doesn't end with a \n
  • [ -z "${line// }" ] trims whitespace before checking if the string is empty
  • [[ $line == "#"* ]] looks for strings beginning with #

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment