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

Re: setting IFS to new line doesn't work while searching?



On Fri, Dec 15, 2023 at 12:33:01PM +0000, Albretch Mueller wrote:
> #fndar=($(IFS=$'\n'; find "$sdir" -type f -printf '%P|%TY-%Tm-%Td
> %TI:%TM|%s\n' | sort --version-sort --reverse))

> the array construct ($( ... )) is using the space (between the date
> and the time) also to split array elements,

Yeah, no.  That's not how it works.

You're setting IFS *inside* the command substitution whose value is
what you're trying to word-split.  It needs to be set outside.

In addition to word splitting, an unquoted command substitution's
output is going to undergo filename expansion (globbing).  So you
would also need to disable that.

More to the point, bash has a 'readarray' command which does what you
*actually* want:

    readarray -t fndar < <(find "$sdir" ...)

This avoids all of the issues with word splitting and globbing and
setting/resetting the IFS variable, and is more efficient as well.

BTW, readarray is a synonym for 'mapfile'.  You may use either spelling.


Reply to: