Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script variable passing to find

I have a simple bash script where I generate some temporary files using split, do some processing and then try to track down all the files at the end and merge them

rand_int=$RANDOM
split -d -l $n_lines_split $1 $rand_int   #works fine

for f in $(find . -amin -200 -regex '.*$rand_int.*' ); do 
    (some processing here) ; 
done

My problem is that in the find command $rand_int is interpreted literally, whereas I want to use the variable's value.

like image 655
lonestar21 Avatar asked Feb 22 '23 01:02

lonestar21


2 Answers

In the shell, single-quotes (') cause what's inside to be interpreted literally. What you want to do is use double-quotes (") around the expression with $rand_int.

So for the find expression:

find . -amin -200 -regex ".*$rand_int.*"
like image 64
Dan Fego Avatar answered Mar 04 '23 12:03

Dan Fego


use " " instead of ''

for f in $(find . -amin -200 -regex ".*$rand_int.*" ); do 
like image 44
user237419 Avatar answered Mar 04 '23 14:03

user237419