Do not for-loop over find command substitution output

This product is not supported for your selected Datadog site. ().

Metadata

ID: bash-code-quality/dont-for-loop-over-find-output

Language: Bash

Severity: Warning

Category: Code Style

Description

A for loop over $(find ...) (or backticks) relies on word splitting. Filenames with spaces or glob characters break the loop or match wrong paths.

Prefer find -exec, find -print0 with while read -d '', or globs where appropriate.

Non-Compliant Code Examples

#!/bin/bash
for f in $(find . -name '*.mp3'); do
  play "$f"
done

for g in `find . -type f`; do
  echo "$g"
done

for h in $(find . 2>/dev/null); do echo "$h"; done

Compliant Code Examples

#!/bin/bash
find . -name '*.txt' -exec cat {} \;
while IFS= read -r -d '' f; do echo "$f"; done < <(find . -print0)
for f in *.txt; do echo "$f"; done
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security