Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: playing with IFS



On Sun 12 Apr 2026, at 16:41, Ray Andrews wrote:
> Trying to optimize some code:
>
> % output=( ${(f) "$( print -rl $output | sed [cut] )" } )
>
> ... replace with hear document:
>
> OLDIFS=$IFS; IFS=$'\n'
> % output=( $( sed [cut]' <<< $output ) )
> IFS=$OLDIFS
>
> ... probably better, but it won't work without tinkering with IFS.

you felt the need to mess with IFS because you took out the ${(f)...},
not because you added the here string. if you really have to use sed for
some reason, just change that back

but if by 'optimize' you mean (a) make it faster (albeit imperceptibly
unless you're running it hundreds of times) or (b) make it more
idiomatic, you should replace the sed itself using zsh's native
string-replacement facilities

assuming output is already an array:

  output=( ${output/pat/repl} )

if it's a string of \n-terminated records:

  output=( ${${(f)output}/pat/repl} )

you almost never need to mess with IFS like that in zsh. if you did need
to you could do

  () { local IFS=$'\n'; ... }

you should also guard variable arguments to commands with -- (or with
zsh built-ins you can use a single -):

  print -rl -- $output

dana




Messages sorted by: Reverse Date, Date, Thread, Author