help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Array Expansion


From: J.B.
Subject: Re: [Help-bash] Array Expansion
Date: Sun, 25 Jun 2017 13:31:09 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1

On 06/25/2017 01:17 PM, João Eiras wrote:
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.

Thanks. Setting IFS to $'\x0a' fixed the way my script handles filenames with spaces.



reply via email to

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