help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf '%s\n' "$@" versus <<< redirection


From: goncholden
Subject: Re: printf '%s\n' "$@" versus <<< redirection
Date: Mon, 20 Feb 2023 13:08:48 +0000

------- Original Message -------
On Tuesday, February 21st, 2023 at 12:42 AM, Greg Wooledge <greg@wooledge.org> 
wrote:


> On Mon, Feb 20, 2023 at 12:31:11PM +0000, goncholden wrote:
> 
> > Does the problem of disappearing variables exist in
> > calls to awk ?
> > 
> > for arg in "$@"; do
> > printf '%s\n' "$arg" | awk '...'
> > done
> 
> 
> Both printf and awk will be executed in subshells. However, awk is
> already an external program, so it's always executed as a separate
> process. awk has its own variables which are never shared back to
> the script from which you call it.
> 
> > or should one use
> > 
> > for arg in "$@"; do
> > awk '...' < <(printf '%s\n' "$arg")
> > done
> 
> 
> That wouldn't change anything. awk is still an external program, and
> is therefore still executed as a separate process, with its own private
> variables and so on.

That would mean that 

printf '%s\n' "$arg" | awk '...'

can be used, because 

awk '...' < <(printf '%s\n' "$arg") 

would not actually improve anything.  Right ?

 
> > There is also the possibility of putting the awk command in the process 
> > substitution, not the printf command:
> > 
> > for arg in "$@"; do
> > while read ...; do
> > ...
> > done < <(awk '...' <<< "$arg")
> > done
> 
> 
> Without knowing what your awk command does it's hard to give advice,
> but it might be more efficient if you call it once instead of once
> per argument.
> 
> theone() {
> printf '%s\n' "$@" | awk 'whatever'
> }
> 
> This is contingent upon your awk command being able to handle the full
> input stream sent in this way.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]