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

Metadata

ID: bash-code-quality/avoid-iteration-over-command-output

Language: Bash

Severity: Notice

Category: Code Style

Description

A for loop over command output (e.g. $(ls ...) or backticks) splits on whitespace and can break on filenames with spaces or glob characters. Prefer globs (e.g. for f in dir/*) or find (e.g. find . -name '*.mp3' -exec some command {} \;) instead.

Non-Compliant Code Examples

#!/bin/bash
for f in $(ls); do echo "$f"; done
for g in `ls -A`; do echo "$g"; done
for h in $(ls 2>/dev/null); do echo "$h"; done

Compliant Code Examples

#!/bin/bash
ls
for f in ./*.txt; do echo "$f"; done
find . -name '*.mp3' -exec some command {} \;
while IFS= read -r -d '' f; do echo "$f"; done < <(find . -maxdepth 1 -print0)
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