LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Can xargs's initial-arguments expand? (https://www.linuxquestions.org/questions/linux-general-1/can-xargss-initial-arguments-expand-713370/)

virteman 03-21-2009 09:37 AM

Can xargs's initial-arguments expand?
 
hello, why can't the xargs's intial-arguments which are passed to any command expanded?

First I Can list the file with a wild expansion.

Code:

virteman@ubuntu:/tmp$ ls -l tmp*
-rw------- 1 root root 0 2009-03-21 18:50 tmp.yWkOcZ6220

Second I echo the wild expansion pipe to xargs and then list it. But the exception occur to here.

Code:

virteman@ubuntu:/tmp$ echo 'tmp*' |xargs -t ls -l
ls -l tmp*
ls: cannot access tmp*: No such file or directory

Then I add bash -c to the list command , And it seams OK.

Code:

virteman@ubuntu:/tmp$ echo 'tmp*' |xargs -t -I{}  bash -c 'ls -l {}'
bash -c ls -l tmp*
-rw------- 1 root root 0 2009-03-21 18:50 tmp.yWkOcZ6220

I want to know why I cannot use the second command?

druuna 03-22-2009 04:49 AM

Hi,

If you remove the quotes it works:

Quote:

touch tmp.yWkOcZ6220

ls -l tmp*
-rw-r----- 1 druuna internet 0 Mar 22 10:45 tmp.yWkOcZ6220

echo tmp* | xargs ls -l
-rw-r----- 1 druuna internet 0 Mar 22 10:45 tmp.yWkOcZ6220

echo 'tmp*' | xargs ls -l
ls: tmp*: No such file or directory

echo "tmp*" | xargs ls -l
ls: tmp*: No such file or directory
Without the quotes bash will expand the tmp* part first before giving it to echo, the other 2 (with the double/single quotes) prohibit bash from expanding and tmp* is echoed as is.

Hope this clears things up a bit

virteman 03-25-2009 03:58 AM

Quote:

Originally Posted by druuna (Post 3483834)
Hi,

If you remove the quotes it works:



Without the quotes bash will expand the tmp* part first before giving it to echo, the other 2 (with the double/single quotes) prohibit bash from expanding and tmp* is echoed as is.

Hope this clears things up a bit

yes, of course,it works. In fact I want to xargs pass "tmp*" this expression to its later call command , and then the command can let bash expand "tmp*", at last the command can run with the arguments as it runs in a common command line .


All times are GMT -5. The time now is 02:11 AM.