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.

1 Answer 1


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
  • 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, 2013 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .