Get argument from pipe

前端 未结 3 2255
清酒与你
清酒与你 2021-02-20 04:28

Consider having the results from the pipe:

find .

Now I would like to access in the second command behind the pipe what is actually piped (inpu

3条回答
  •  一整个雨季
    2021-02-20 04:56

    Short answer: You can print each file twice using sed:

    find . | sed 's/.*/& &/'
    

    Sed can edit lines as they are inputted. The above command says s (substitute) .* (the entire line) & & (with itself, twice).

    Longer answer: When you pipe one program into another, you're connecting the first program's standard output stream to the second program's standard input stream. Anything the first program prints will be treated as input for the second program.

    Unfortunately for your example, the input doesn't come in line-by-line chunks that map nicely to a hypothetical $args variable. It comes in a big monolithic stream. If you want to print each line of the stream twice, you can use sed (which is a Stream EDitor), but it's just doing line-by-line replacements.

提交回复
热议问题