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?

有帮助吗?

解决方案 2

array=()
while read line; do
    array+=("$line")
done < "$file"

其他提示

Bash 4 introduced readarray:

readarray -t array < "$file"

and you're done.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top