From 14a233101aa43500c8a2c1e573facd4e273802f5 Mon Sep 17 00:00:00 2001 From: adigitoleo Date: Sat, 12 Feb 2022 21:20:59 +1100 Subject: [PATCH 1/2] 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`. From 6f63d0492fe2a8dc24d00f393019529ea597b5d8 Mon Sep 17 00:00:00 2001 From: Leon Date: Sat, 7 Jan 2023 12:39:09 +1100 Subject: [PATCH 2/2] Update README.md Change command output example to avoid subshell use Co-authored-by: wael444 <40663@proton.me> --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3cd50cc..6853252 100644 --- a/README.md +++ b/README.md @@ -653,11 +653,11 @@ done < "file" ## Loop over the output of a command ```shell -command|while IFS= read -r line || [ -n "$line" ]; do +while IFS= read -r line || [ -n "$line" ]; do printf '%s\n' "$line" -done -``` - +done <