Home > other >  why this command not working?(pipe, redirection)
why this command not working?(pipe, redirection)

Time:09-18

bash-3.2$ ls | grep Makefile > a.txt | cat a.txt

why this don't work?? "Makefile" is exist.

CodePudding user response:

There is no output from the grep command since you are redirecting it to a file. Therefore the pipe gets closed before the cat a.txt actually gets called. As per my comment, use && instead of that last |.

CodePudding user response:

Do do what you want which is to save the output of a command to a file but still print to stdout use tee. (also I like to do ls -1 to make sure only one item per line is printed this is not necessary but force of habit)

 ls -1 | grep Makefile | tee a.txt
  • Related