3

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.

Bo Chen
  • 131

3 Answers3

3

The following will work but probably has some negative security implications:

echo "You have" * "in current folder"

IMO a better way, but requiring two lines would be:

files=(*)
echo "You have ${files[@]} in curent folder"

With printf:

files=(*)
printf '%s ' "You have ${files[@]} in current folder"
Kusalananda
  • 333,661
jesse_b
  • 37,005
  • I think ${files[*]} should be prefered over ${files[@]}, see https://stackoverflow.com/a/3355375/2779871 – Jules Sam. Randolph Apr 12 '18 at 13:27
  • 2
    @JulesRandolph: Negative, @ should be used over * in almost every instance, * is delimited by your IFS which could cause undesirable results especially in this instance. OP wants the files printed on a single line so if IFS was set to \n then it would not provide the output he wants...or if IFS was set to basically anything but the default... – jesse_b Apr 12 '18 at 13:31
  • nice.... thanks!! how to concatenate indefinite parameters passed in? printf 'you have passed in %s %s %s ??.... paramters' – Bo Chen Apr 12 '18 at 13:32
  • @BoChen This will concatenate all of the files regardless of how many there are. – jesse_b Apr 12 '18 at 13:33
  • @Jesse_b yeah I've already tried it out. your solution works well. I'm asking the printf questions because I'm interested to know that as well.. – Bo Chen Apr 12 '18 at 13:37
  • @Jesse_b You are right ! – Jules Sam. Randolph Apr 12 '18 at 13:41
  • @Jesse_b thanks again for your answer. why does ls | xargs printf 'you have $@ in current folder' not work? – Bo Chen Apr 12 '18 at 14:00
  • @BoChen It "works", it just doesn't do what you think it should do. Also: https://unix.stackexchange.com/questions/128985/why-not-parse-ls – Kusalananda Apr 12 '18 at 15:01
  • @Jesse_b However, here are some arguments against it: https://github.com/koalaman/shellcheck/wiki/SC2145 – Jules Sam. Randolph Apr 16 '18 at 18:03
1

For what it's worth:

echo You have $(ls) in the current folder 
nohillside
  • 3,251
  • thanks. works well! Can you help answer my second question as well ? i.e. ls |xargs printf 'you have %s %s %s %s in current folder' way, how to make it work? – Bo Chen Apr 12 '18 at 13:46
0

I think Jesse_b's solution is the best, but if you want something like xargs you can use GNU Parallel:

ls | parallel --xargs echo you have {} in current folder
Ole Tange
  • 35,514