Piping and Redirection

前端 未结 5 2129
挽巷
挽巷 2020-12-01 03:15

What is exact difference between piping and redirection?

Where should we use piping and where should we use redirection?

How they internall

5条回答
  •  甜味超标
    2020-12-01 03:33

    Piping directs the output of a program to another program.

    For example:

    ls * | grep "name"
    

    Pipes the names of all files in the current directory to grep. Re-direction directs or appends output to a file.

    ls * > file  # writes all file names in current directory to the "file"
    ls * >> file # appends all files names in current directory to the "file"
    

    Piping saves you the hassle of having to write to a file, then read from a file to execute a program on the output of another program.

    ls * > file
    grep "name" file
    

    is equivalent to

    ls * | grep "name"
    

    As for how it they work internally, I am only learning that my self now. But I found this link which offers some discussion on it.

    How Does Piping Work in Linux?

    You should use piping if you want to pass outputs between programs; use redirection if you want to write to a file.

提交回复
热议问题