-3

I have variable and its contains of string

LST_FILE='find \"$2\" \"${TYPE[@]}\" \"${NAME[@]}\" -mmin +\"$HOUR_TO_MIN\"'

how to turn related string into command ? is it fine using eval ? or there's any substitute for eval because I've been read that eval is not recommended to use..

eval $LST_FILE

because I plan to do below command,

LST_FILE+='-delete'
eval $LST_FILE
  • 1
    Use an array; see this, this, this, and BashFAQ #50. Avoid eval, it is a massive footgun. – Gordon Davisson Aug 15 '21 at 00:57
  • hi i already use an array for the option case (${TYPE[@]}, ${NAME[@]}. i'm just wondering that eval is really dangerous or just misunderstood.. – user3679987 Aug 15 '21 at 13:58
  • eval is dangerous in large part because it's misunderstood. It takes the shell's normal parsing process -- which is rather complicated and confusing -- and repeats is, making the whole thing even more complicated and confusing. It can be used safely if you understand shell parsing well, but it tends to get used as a substitute for understanding shell parsing... and doing more of that thing you don't understand is rarely a good idea. (And if you do understand shell parsing well, you usually know of better solutions to whatever problem you're facing.) – Gordon Davisson Aug 15 '21 at 17:29

1 Answers1

1

Yes, eval is not recommended and it has already been answered here why.

Now, I'm not sure what you are trying to do as you don't provide any context, code and the expected result, but if you want to manipulate data so you execute find based on certain conditions then you could just execute like this:

LST_FILE_DELETE='-delete'
find "$2" "${TYPE[@]}" "${NAME[@]}" -mmin +"$HOUR_TO_MIN" "${LST_FILE_DELETE}"

Normally when a solution seems complex or you have to go through several workarounds, then you should probably think about the problem again and how to solve it, most likely you'll find a better way to solve it without using workarounds.

0x00
  • 111
  • hmm ok thanks for the suggestion, i'm just wonder that eval is really that dangerous or just misunderstood, probably in some several case that we can use eval well – user3679987 Aug 15 '21 at 14:00