Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I expect the command

ls -d doc/* | grep -P "<some_pattern>" | xargs bash -ic echo

to do the same as:

ls -d doc/* | grep -P "<some_pattern>" | xargs echo

that is, to give me the files matched by ls -d doc/* | grep -P "<some_pattern>", only separated by spaces instead of newlines.

But I get only a newline as output instead.

Why is this? How can I fix the first command to do what I want?

By the way, I’m realling using zsh instead of bash, but neither will work.

I actually need this to print a bunch of files whose file names are matched by the pattern specified by "grep" and should be printed using an .zshrc-aliased command.

share|improve this question
up vote 4 down vote accepted

from the bash manpage

   -c string If the -c option is present,  then  commands  are  read  from
             string.   If  there  are arguments after the string, they are
             assigned to the positional parameters, starting with $0.


 $ echo a b c d e f g | xargs bash -ic echo

 $ echo a b c d e f g | xargs bash -ic 'echo $0 $@'
 a b c d e f g
share|improve this answer
Good answer, beat me to the punch 8-) – slm Sep 9 '13 at 19:46
This works! Great, thanks. I was missing the existence of positional parameters and just overlooked that part of the man pages. My bad! – k.stm Sep 9 '13 at 20:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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