basename problems with xargs/find and command substitution

Bug #523453 reported by Trev Peterson
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
coreutils (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

Binary package hint: coreutils

basename does not return the correct basename when executed using command substitution from find (-exec) or xargs (dirname does).

works using find -exec directly
$ find . -type f -iname '*.jpg' -exec basename {} \;
Patio before.JPG
Patio1-1.JPG
Patio1-2.JPG
Patio1-3.JPG

does not work using find -exec using command substitution
$ find . -type f -iname '*.jpg' -exec printf "$( basename {} ) \n" \;
./Patio before.JPG
./Patio1-1.JPG
./Patio1-2.JPG
./Patio1-3.JPG

dirname works using find -exec using command substitution
$ find . -type f -iname '*.jpg' -exec printf "$( dirname {} ) \n" \;
.
.
.
.

works direct from xargs
printf "./Patio1-1.JPG\n" | xargs -0 -I{} basename {}
Patio1-1.JPG

does not work from xargs with command substitution (again dirname does)
printf "./Patio1-1.JPG\n" | xargs -0 -I{} printf " fullname:{} basename:$( basename "{}" ) dirname:$( dirname "{}" )\n"
 fullname:./Patio1-1.JPG
 basename:./Patio1-1.JPG
 dirname:.

System information:
9.10 karmic
ii findutils 4.4.2-1 utilities for finding files--find, xargs
ii coreutils 7.4-2ubuntu1 The GNU core utilities
uname -a
Linux aegir 2.6.31-19-generic #56-Ubuntu SMP Thu Jan 28 02:39:34 UTC 2010 x86_64 GNU/Linux

$ which basename
/usr/bin/basename

Let me know if you need anything else to help with this. Thanks,

Revision history for this message
C de-Avillez (hggdh2) wrote :

Thank you for opening this bug and helping make Ubuntu better.

This had me stumped for a while, and I queried about it (all the while with this firm believe the answer was staring at me). And I did get an answer. Indeed, this is not a bug, but normal shell processing...

Here's the deal: when you have a $(...) construct, the contents are passed to the shell. Shell variables are resolved as usual, and the rest is left as is. It happens that '{}' is *not* a shell variable -- so it is left as is.

*When* the shell returns... then '{}' gets to be magic again, and -exec processing replaces it by the correct value.

This can be easily seen by running the following:

   echo find . -type f -exec echo $(basename {}) \;

As such, marking Invalid.

p.s. And I *knew* I was missing the answer ;-) -- Thank you, Bob!

Changed in coreutils (Ubuntu):
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.