[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Pipe using subshells, variables not saved
From: |
Greg Wooledge |
Subject: |
Re: Pipe using subshells, variables not saved |
Date: |
Mon, 6 Mar 2017 08:10:39 -0500 |
User-agent: |
Mutt/1.4.2.3i |
On Fri, Mar 03, 2017 at 10:04:23PM -0800, Misaki wrote:
> > reason for piping:
> > echo wat | for i in $(< /dev/stdin); do declare -g new="$i"; done
> >
> > (using find instead of echo, not sure if better way to loop)
while IFS= read -r file; do blah "$file"; done < <(find . -type f)
That's compatible with any filenames that don't contain newlines.
To be 100% compatible with ALL possible filenames, you need to use
a NUL-delimited stream. On GNU/BSD systems:
while IFS= read -rd '' file; do blah "$file"; done < <(find . -type f -print0)
On recent-enough POSIX systems:
while IFS= read -rd '' file; do blah "$file"; done \
< <(find . -type f -exec printf '%s\0' {} +)
See also http://mywiki.wooledge.org/BashFAQ/024
See also http://mywiki.wooledge.org/DontReadLinesWithFor