bash loop skip commented lines

This is an old question but I stumbled upon this problem recently, so I wanted to share my solution as well.

If you are not against using some python trickery, here it is:

Let this be our file called "my_file.txt":

this line will print
this will also print # but this will not
# this wont print either
      # this may or may not be printed, depending on the script used, see below

Let this be our bash script called "my_script.sh":

#!/bin/sh

line_sanitizer="""import sys
with open(sys.argv[1], 'r') as f:
    for l in f.read().splitlines():
        line = l.split('#')[0].strip()
        if line:
            print(line)
"""
echo $(python -c "$line_sanitizer" ./my_file.txt)

Calling the script will produce something similar to:

$ ./my_script.sh
this line will print
this will also print

Note: the blank line was not printed

If you want blank lines you can change the script to:

#!/bin/sh

line_sanitizer="""import sys
with open(sys.argv[1], 'r') as f:
    for l in f.read().splitlines():
        line = l.split('#')[0]
        if line:
            print(line)
"""
echo $(python -c "$line_sanitizer" ./my_file.txt)

Calling this script will produce something similar to:

$ ./my_script.sh
this line will print
this will also print



while read line; do
  case "$line" in \#*) continue ;; esac
  ...
done < /tmp/my/input

Frankly, however, it is often clearer to turn to grep:

grep -v '^#' < /tmp/myfile | { while read line; ...; done; }

Tags:

Shell

Bash