Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - reading wildcard patterns from a file into an array

I want to read a list of patterns that may (probably do) contain wildcards from a file

The patterns might look like this:

/vobs/of_app/unix/*
/vobs/of_app/bin/*

etc

My first attempt was to do this:

old_IFS=$IFS
IFS=$'\n'
array=($(cat $file))

This worked fine when the patterns did not match anything in the filesystem, but when they did match things in the filesystem, they got expanded so instead of containing the patterns, my array contained the directory listings of the specified directories. This was no good.

I next tried quoting like this

array=("$(cat $file)")

But this dumped the entire contents of the file into the 0th element of the array.

How can I prevent it from expanding the wildcards into directory listings while still putting each line of the file into a separate array element?

like image 402
timcrall Avatar asked Jun 28 '26 19:06

timcrall


1 Answers

Bash 4 introduced readarray:

readarray -t array < "$file"

and you're done.

like image 93
that other guy Avatar answered Jun 30 '26 16:06

that other guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!