This sed script removes all comments and empty lines from shell scripts.

sed -e 's/^[[:blank:]]*#.*//' -e '/^[[:blank:]]*$/d'

Bug: it deletes also the leading #!/bin/bash line in a shell script.

Explanation

Delete all lines that start with zero or more consecutive white space character (spaces, tabs, cr etc.) followed by the hash character, which is followed by zero or more arbitrary characters.

Also delete all lines that onlcy contains whitespace characters.

Example

An arbitrary shell script file, with comments

cat apt-compat 

#!/bin/sh

set -e

# Systemd systems use a systemd timer unit which is preferable to
# run. We want to randomize the apt update and unattended-upgrade
# runs as much as possible to avoid hitting the mirrors all at the
# same time. The systemd time is better at this than the fixed
# cron.daily time
if [ -d /run/systemd/system ]; then
    exit 0
fi

check_power()
{
    # laptop check, on_ac_power returns:
    #       0 (true)    System is on main power
    #       1 (false)   System is not on main power
    #       255 (false) Power status could not be determined
    # Desktop systems always return 255 it seems
    if which on_ac_power >/dev/null 2>&1; then
        on_ac_power
        POWER=$?
        if [ $POWER -eq 1 ]; then
            return 1
        fi
    fi
    return 0
}
...

Here is the output: same shell script file with comments removed.

cat apt-compat | sed -e 's/^[[:blank:]]*#.*//' -e '/^[[:blank:]]*$/d'

set -e
if [ -d /run/systemd/system ]; then
    exit 0
fi
check_power()
{
    if which on_ac_power >/dev/null 2>&1; then
        on_ac_power
        POWER=$?
        if [ $POWER -eq 1 ]; then
            return 1
        fi
    fi
    return 0
}
...

Related articles