14 Recursiveness
John Gardner edited this page 2021-12-22 16:14:02 +11:00

By default, shellcheck does not support a -r option. The reason for this is that there are different matching patterns for different files.

Script could have a .sh extension, no extension and have a range of shebang lines (which have their own testing format):

  • #!/bin/bash
  • #!/bin/sh
  • #!/usr/bin/env bash
  • #!/usr/bin/env sh
  • etc...

The solution for this problem is to use shellcheck in combination with the find or grep command.

By file extension

To check files with one of multiple extensions:

# Bash 4+
# nullglob will prevent one of the extension patterns from appearing in the arg list
# if they don't match.
# dotglob will match shell scripts in hidden directories.
shopt -s nullglob dotglob
shellcheck /path/to/scripts/**.{sh,bash,ksh,bashrc,bash_profile,bash_login,bash_logout}

# POSIX
find /path/to/scripts -type f \( -name '*.sh' -o -name '*.bash' -o -name '*.ksh' -o -name '*.bashrc' -o -name '*.bash_profile' -o -name '*.bash_login' -o -name '*.bash_logout' \) \
  | xargs shellcheck

By shebang

To check files whose shebang indicate that they are sh/bash/ksh scripts:

# POSIX
find /path/to/scripts -type f -exec grep -Eq '^#!(.*/|.*env +)(sh|bash|ksh)' {} \; \
  | xargs shellcheck

With Docker

docker run --volume /path/to/scripts:/mnt koalaman/shellcheck-alpine sh -c "find /mnt -name '*.sh' | xargs shellcheck"

To exclude file/directory

To exclude files or directories from the search(find) results use ! -path. To exclude vendor directory for example:

find /path/to/scripts -type f \( -name '*.sh' \) ! -path '*/vendor/*' | xargs shellcheck -x -s sh