How To Piping into Functions

Bash can pipe data into functions.

There is one stdin for the whole script, and the function can read the data for this.

 1function pipeMe1 {
 2    # Just read a line
 3    read var
 4    echo "One line read, it's :${var}"
 5
 6    # More stdin can be left here, `read` more of it or cat the rest.
 7    # cat may hang if stdin is closed
 8}
 9
10function pipeMe2 {
11    # Now read the rest of the stdin
12    cat
13}
14
15function pipeMe3 {
16    # Now read the rest of the stdin
17    cat <<EOF
18    Here is my heredoc
19    $(cat)
20    I've wrapped that stdin with more stdin
21EOF
22}
23
24pipeMe1 <<EOF
25line 1
26line 2
27EOF
28
29pipeMe2 <<EOF
30line 1
31line 2
32EOF
33
34pipeMe3 <<EOF
35line 1
36line 2
37EOF