Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to assign output of find into array

Tags:

linux

bash

shell

In linux shell scripting I am trying to set the output of find into an array as below

#!/bin/bash
arr=($(find . -type -f))

but it give error as -type should contain only one character. can anybody tell me where is the issue.

Thanks

like image 233
agarwal_achhnera Avatar asked Aug 12 '14 08:08

agarwal_achhnera


1 Answers

If you are using bash 4, the readarray command can be used along with process substitution.

readarray -t arr < <(find . -type f)

Properly supporting all file names, including those that contain newlines, requires a bit more work, along with a version of find that supports -print0:

while read -d '' -r; do
    arr+=( "$REPLY" )
done < <(find . -type f -print0)
like image 182
chepner Avatar answered Nov 14 '22 12:11

chepner