Re: basename is not working as expected if invoked in `` inside a find subcommand
[email protected] (Paul Jarc)
| Newsgroups | gmane.comp.gnu.utils |
|---|---|
| Organization | What did you have in mind? A short, blunt, human pyramid? |
| Message-ID | <[email protected]> |
"Andreas R." <[email protected]> wrote: > find dir1 -type f -exec sh -c 'echo "{}" dir2/"`basename "{}"`"' \; > it works, but I still don't understand completely why it works. Actually, it still won't work in some cases. If a filename contains a double quote, like 'foo" "bar', the shell will see this command for -c: echo "foo" "bar" dir2/"`basename "foo" "bar"`" which isn't what you want. This will handle any flename properly: find dir1 -exec sh -c 'diff -q "$0" dir2/"`basename "$0"`"' {} \; The difference here is that {} is substituted by find, before sh is invoked, so if {} appears within the -c command, sh will interpret any special characters in the filename as shell syntax, not as part of the filename. Keeping {} as a separate argument to sh, and referring to it as a variable from within the command, prevents any such problems. > And where do you learn such tricks from? :) Experience, mostly. paul