From 14a233101aa43500c8a2c1e573facd4e273802f5 Mon Sep 17 00:00:00 2001 From: adigitoleo Date: Sat, 12 Feb 2022 21:20:59 +1100 Subject: [PATCH] Add example for looping over command output --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 8032be5..3cd50cc 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ See something incorrectly described, buggy or outright wrong? Open an issue or s * [Loop over a (*small*) range of numbers](#loop-over-a-small-range-of-numbers) * [Loop over a variable range of numbers](#loop-over-a-variable-range-of-numbers) * [Loop over the contents of a file](#loop-over-the-contents-of-a-file) + * [Loop over the output of a command](#loop-over-the-output-of-a-command) * [Loop over files and directories](#loop-over-files-and-directories) * [VARIABLES](#variables) * [Name a variable based on another variable](#name-a-variable-based-on-another-variable) @@ -649,6 +650,14 @@ while IFS= read -r line || [ -n "$line" ]; do done < "file" ``` +## Loop over the output of a command + +```shell +command|while IFS= read -r line || [ -n "$line" ]; do + printf '%s\n' "$line" +done +``` + ## Loop over files and directories Don’t use `ls`.