Pipe, standard input and command line arguments in Bash

前端 未结 4 887
轻奢々
轻奢々 2020-12-01 15:06

Consider:

command1 | command2

Is the output of command1 used as standard input of command2 or as command line arguments to command2?

<
4条回答
  •  臣服心动
    2020-12-01 15:42

    It's used as the stdin.

    Try:

    grep "hehe" - $(cat test.sh)
    

    That might be wrong; I can't test it out on this computer. If you do it without the pipe like you tried, grep treats the last argument as a filename, ie, looks for a file called [contents of test.sh]. If you pass it a - (or don't put a last argument), you tell it to use stdin as the file.

    You can also just pass grep a file to scan through:

    grep "hehe" test.sh
    

    ...but you seem to be asking more of a generalized bash question, not really a grep usage question, so that's probably not too helpful.

提交回复
热议问题