0

The following command:

echo ./test/test2/test23 | xargs -I "{}" echo `dirname "{}"`  

Outputs: . (not as expected)

The following command:

echo ./test/test2/test23 | xargs -I "{}" echo `dirname ./test/test2/test23` 

Outputs: ./test/test2 (as expected)

The following command:

echo ./test/test2/test23 | xargs -I "{}" echo `echo "{}"`  

Outputs: ./test/test2/test23 (as expected)

Why is the first command not outputting ./test/test2?

0

1 Answer 1

0

The reason for the unexpected output is that the subshell substitution of dirname "{}" is being executed first (having an output of .), making the command effectively echo "./test/test2/test23" | xargs -I "{}" echo .. Since there is no match for xargs' specified marker of {}, there is nothing replaced, and echo . runs as expected, with output of ..

5
  • Makes sense. Do you have a suggestion on how to fix it? Feb 23 at 5:55
  • echo "./test/test2/test23" | xargs -I "{}" dirname "{}" seems to have the intended result.
    – DopeGhoti
    Feb 23 at 5:57
  • ah yep, fair. I'm trying to solve this problem: find . -name "*" -type f | xargs -I "{}" cp "{}" `dirname "{}"`. Would you be able to help me find an equivalent to this please? Feb 23 at 5:58
  • That would be beyond the scope of this question, but if you were to submit a new question with your needs explained, I'm confident it will get addressed.
    – DopeGhoti
    Feb 23 at 6:00
  • Sure, here's a link to the new question: unix.stackexchange.com/questions/736560/… Feb 23 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.