3

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

4

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.

so...

 $ 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
1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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