Kawson
Kawson

Reputation: 136

Linux bash script: How to put command line into variable?

I have a questions about bash script, i want to put part of find expression to variable. I show on example better what i want to. I have something like this:

find $DIR -type f -name "$NAME" $SIZE $CONTENT_COM;;

and i want to put into $CONTENT_COM something like this: (exactly like this)

-exec grep -l "$CONTENT" {} +

For $SIZE i made this:

SIZE=${SIZE/$SIZE/-size $SIZE};

and i wanted to make this for $CONTENT_COM (it looks similar to what i want, just change -size $SIZE etc to look like this

CONTENT_COM=${CONTENT_COM/$CONTENT/-exec grep -l "$CONTENT" {} +} 

but it doesnt work. ({} +} <---- error in editor) Is there any way to put such expression to variable then use it ?

Upvotes: 0

Views: 213

Answers (1)

l0b0
l0b0

Reputation: 58768

Yes, you can use arrays to build up commands with arbitrary arguments, for example:

search_term='some regex'
content_command=('grep' '-l' "$search_term")
find . -exec "${content_command[@]}" {} +

Also, Use More Quotes™!

Upvotes: 2

Related Questions