help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Array Expansion


From: João Eiras
Subject: Re: [Help-bash] Array Expansion
Date: Sun, 25 Jun 2017 22:17:57 +0200

On 25 June 2017 at 21:25, J.B. <address@hidden> wrote:
> Expanding an array for use in a for-loop behaves differently depending on
> how the array was created. How can I get bash to expand using a different
> delimiter? I tried `find -print0' after setting IFS= but bash just ignores

Your array is not being expanded differently in the for-loop. You're
creating it incorrectly.

What's happening ?
First bash evaluates the command $() and substitutes that expression
with the value from stdout.
Secondly, it breaks apart that string into tokens given the values in IFS.
Thirdly it assigns each token to a different position in the array.

This is most likely what you want:

IFS=$'\x0a'
listoffiles=( $(find ...) )

Or if you're picky about wanting not to treat the newline as a
separator, this helps you further
https://stackoverflow.com/questions/1116992/capturing-output-of-find-print0-into-a-bash-array

On 25 June 2017 at 22:03, Andy Chu <address@hidden> wrote:

> Example of subshell:
>
> find -print0 | (i=0; while read -d '' line; do echo $line; ((i++)); done;
> echo $i )
>

You're reading the values into a sub-shell, which are not inherited by
the main shell.



reply via email to

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