0

I have a variable that contains a find command

LST_FILE=$(find . -type f \( -name -o '*xml*' -o -name -o '*log*' \) -mmin 180)

Is it possible to append the command ? I mean like this

LST_FILE+=$(-delete)

or probably

DEL=$(-delete)
LST_FILE+=${DEL}

I need to know because I have several find commands that need to perform and it has different options for each commands, so I decided to put the command into variable and plan to append it with of each options regarding to the condition..

1 Answers1

-1

First of all, your find command is wrong. You want this:

find . -type f \( -name  '*xml*' -o -name '*log*' \) -mmin 180

Next, your variable does not contain a find command, it contains the output of a find command. I suspect what you really want is this:

find_command='find . -type f ( -name  *xml* -o -name *log ) -mmin 180'
del="-delete"

Then, to add the -delete to the command, you can do:

find_command="$find_command $del"

And to execute it, you would simply run:

$find_command

But please read Using shell variables for command options and http://mywiki.wooledge.org/BashFAQ/050. There are a lot of complications when trying to use variables to run commands.

terdon
  • 242,166
  • You have uneven number of single quotes in your find_command= line, and should be using an array (or the positional parameters) rather than a string. – Kusalananda Aug 14 '21 at 15:14
  • Thanks, @Kusalananda, fixed the quotes. And yes, of course you should be using an array, that's what the two references I link to at the end explain. I just also wanted to point out the other issues. This specific case does work as a string, so I thought it would be useful to show how and link to resources showing the Right Way®. – terdon Aug 14 '21 at 15:21