Tutorial by Examples

The following compares two files with diff using process substitution instead of creating temporary files. diff <(curl http://www.example.com/page1) <(curl http://www.example.com/page2)
This feeds a while loop with the output of a grep command: while IFS=":" read -r user _ do # "$user" holds the username in /etc/passwd done < <(grep "hello" /etc/passwd)
# Process substitution with paste command is common # To compare the contents of two directories paste <( ls /path/to/directory1 ) <( ls /path/to/directory1 )
It is well known that you cannot use the same file for input and ouput in the same command. For instance, $ cat header.txt body.txt >body.txt doesn’t do what you want. By the time cat reads body.txt, it has already been truncated by the redirection and it is empty. The final result will be th...
This counts the number of lines in a big file with wc -l while simultaneously compressing it with gzip. Both run concurrently. tee >(wc -l >&2) < bigfile | gzip > bigfile.gz Normally tee writes its input to one or more files (and stdout). We can write to commands instead of fil...
One major aspect of process substitution is that it lets us avoid usage of a sub-shell when piping commands from the shell. This can be demonstrated with a simple example below. I have the following files in my current folder: $ find . -maxdepth 1 -type f -print foo bar zoo foobar foozoo barzoo ...

Page 1 of 1