From 712daf6c49210966694db1c434b30fd6632e45c0 Mon Sep 17 00:00:00 2001 From: Edson Brandi Date: Fri, 7 Nov 2025 21:53:51 +0000 Subject: [PATCH] Fix subshell variable scope issue in file counting scripts The count variable was not being updated because the while loops were running in subshells created by the pipe operator. Variable assignments in subshells don't affect the parent shell, causing count to remain 0. Changes: - list_files.sh: Changed from pipe to here-document with command substitution to keep the while loop in the main shell - list_files_bash.sh: Changed from pipe to process substitution using < <(...) syntax to avoid subshell creation for the while loop Both scripts now correctly display the total number of files found. --- content/chapters/part1/chapter-03.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/chapters/part1/chapter-03.md b/content/chapters/part1/chapter-03.md index 2fe80e2..665278d 100644 --- a/content/chapters/part1/chapter-03.md +++ b/content/chapters/part1/chapter-03.md @@ -2639,12 +2639,13 @@ set -eu cd "${HOME}" count=0 -find . -maxdepth 1 -type f ! -name ".*" -print | while IFS= read -r f; do fname=${f#./} echo "File found: '$fname'" count=$((count + 1)) -done +done << EOF +$(find . -maxdepth 1 -type f ! -name ".*" -print) +EOF echo "Total files found: $count" ``` @@ -2672,12 +2673,11 @@ set -eu cd "${HOME}" count=0 -find . -maxdepth 1 -type f ! -name ".*" -print0 | while IFS= read -r -d '' f; do fname=${f#./} echo "File found: '$fname'" count=$((count + 1)) -done +done < <(find . -maxdepth 1 -type f ! -name ".*" -print0) echo "Total files found: $count" ```