Thijser
Thijser

Reputation: 2633

Concat the results of find

I'm trying to concatenate the results of find in Bash. So I got the following code:

c="sh -content_image in.jpg, style_image "

find t/Pictures -name "*.jpg"| while read line; do
    c=${c}",",
    c=${c}"$line"
    echo $line

 done

echo $c

This first prints all the files in t/Pictures and then the string sh -content_image in.jpg, style_image. While what it's supposed to do is first print all the files and then print that string followed by all of the

So the current output looks like this:

t/Pictures/nature/ActiOn_89.jpg
t/Pictures/nature/ActiOn_27.jpg
t/Pictures/nature/ActiOn_47.jpg
t/Pictures/nature/ActiOn_54.jpg
sh -content_image in.jpg, style_image

but I want it too look like

t/Pictures/nature/ActiOn_89.jpg
t/Pictures/nature/ActiOn_27.jpg
t/Pictures/nature/ActiOn_47.jpg
t/Pictures/nature/ActiOn_54.jpg
sh -content_image in.jpg, style_image t/Pictures/nature/ActiOn_89.jpg,t/Pictures/nature/ActiOn_27.jpg,t/Pictures/nature/ActiOn_47.jpg,t/Pictures/nature/ActiOn_54.jpg

Any idea what I'm doing wrong?

Upvotes: 2

Views: 285

Answers (1)

anubhava
anubhava

Reputation: 785611

You can do:

c="sh -content_image in.jpg, style_image "

while IFS= read -rd '' line; do
    echo "$line"
    c+="$line,"
done < <(find t/Pictures -name "*.jpg" -print0)

# strip trailing comma
c="${c%,}"

Upvotes: 3

Related Questions