0

I want the following assigned to a bash variable.

find . -name "*.zip" -exec echo {} ;

The below will throw warnings and errors, especially with the *(star). Seems ok without the name argument.

#!/usr/bin/bash
cmd="find . -name \"*.zip\" -exec echo {} ;"
$cmd

find: paths must precede expression: echo

I think it is the find command itself that is off?

  • 3
    Also see [BashFAQ/050 (I'm trying to put a command in a variable, but the complex cases always fail!)](https://mywiki.wooledge.org/BashFAQ/050). – pjh Feb 14 '23 at 21:06
  • The problem with that duplicate is the accepted answer there tells how to do what was asked for instead of saying "don't do that". – Ed Morton Feb 14 '23 at 22:03
  • A comment in answer above and the FAQ did give me workaround. set -x...to "print out command" – jacobishere Feb 14 '23 at 22:03
  • 1
    (As is regrettably common for questions tagged 'bash' on Stack Overflow) the accepted and extravagantly upvoted answer to the [duplicate question](https://stackoverflow.com/questions/5615717/how-can-i-store-a-command-in-a-variable-in-a-shell-script) is poor. See [Why should eval be avoided in Bash, and what should I use instead?](https://stackoverflow.com/q/17529220/4154375). – pjh Feb 14 '23 at 22:15

0 Answers0