let's say ls
retunrs file1 file2 dir1 dire2 ...
, I want to print you have file1 file2 dir1 dire2 ... in currnent folder
.
How can I do that?
ls | xargs -i echo 'you have {} in current folder'
prints
you have file1 in current folder you have file2 in current folder you have dir1 in current folder you have dir2 in current folder you have xxx in current folder
also, I have tried
ls |xargs printf 'you have %s %s %s %s in current folder'
but couldn't make it work. as the number of files is indefinite. what is the right syntax for printf
in this case?
ls | xargs printf 'you have $@ in current folder'
is the closest I can get, but it doesn't work.
${files[*]}
should be prefered over${files[@]}
, see https://stackoverflow.com/a/3355375/2779871 – Jules Sam. Randolph Apr 12 '18 at 13:27@
should be used over*
in almost every instance,*
is delimited by yourIFS
which could cause undesirable results especially in this instance. OP wants the files printed on a single line so ifIFS
was set to\n
then it would not provide the output he wants...or ifIFS
was set to basically anything but the default... – jesse_b Apr 12 '18 at 13:31printf
questions because I'm interested to know that as well.. – Bo Chen Apr 12 '18 at 13:37ls | xargs printf 'you have $@ in current folder'
not work? – Bo Chen Apr 12 '18 at 14:00