title: How can I have output from one named pipe fed back into another named pipe?
body: <p>I'm adding some custom logging functionality to a bash script, and can't figure out why it won't take the output from one named pipe and feed it back into another named pipe.</p>
  
  <p>Here is a basic version of the script (<a href="http://pastebin.com/RMt1FYPc" rel="nofollow noreferrer">http://pastebin.com/RMt1FYPc</a>):</p>
  
  <pre><code>#!/bin/bash
  
  PROGNAME=$(basename $(readlink -f $0))
  LOG="$PROGNAME.log"
  PIPE_LOG="$PROGNAME-$$-log"
  PIPE_ECHO="$PROGNAME-$$-echo"
  
  # program output to log file and optionally echo to screen (if $1 is "-e")
  log () {
    if [ "$1" = '-e' ]; then 
      shift
      $@ &gt; $PIPE_ECHO 2&gt;&amp;1 
    else 
      $@ &gt; $PIPE_LOG 2&gt;&amp;1 
    fi
  }
  
  # create named pipes if not exist
  if [[ ! -p $PIPE_LOG ]]; then 
    mkfifo -m 600 $PIPE_LOG
  fi
  if [[ ! -p $PIPE_ECHO ]]; then 
    mkfifo -m 600 $PIPE_ECHO
  fi
  
  # cat pipe data to log file
  while read data; do
    echo -e "$PROGNAME: $data" &gt;&gt; $LOG 
  done &lt; $PIPE_LOG &amp;
  
  # cat pipe data to log file &amp; echo output to screen
  while read data; do
    echo -e "$PROGNAME: $data"
    log echo $data   # this doesn't work
    echo -e $data &gt; $PIPE_LOG 2&gt;&amp;1   # and neither does this
    echo -e "$PROGNAME: $data" &gt;&gt; $LOG   # so I have to do this
  done &lt; $PIPE_ECHO &amp;
  
  # clean up temp files &amp; pipes
  clean_up () {
    # remove named pipes
    rm -f $PIPE_LOG
    rm -f $PIPE_ECHO
  }
  #execute "clean_up" on exit
  trap "clean_up" EXIT 
  
  log echo "Log File Only"
  log -e echo "Echo &amp; Log File"
  </code></pre>
  
  <p>I thought the commands on line 34 &amp; 35 would take the <code>$data</code> from <code>$PIPE_ECHO</code> and output it to the <code>$PIPE_LOG</code>.  But, it doesn't work.  Instead I have to send that output directly to the log file, without going through the <code>$PIPE_LOG</code>.</p>
  
  <p>Why is this not working as I expect?</p>
  
  <p>EDIT: I changed the shebang to "bash".  The problem is the same, though.</p>
  
  <p>SOLUTION:  <a href="https://stackoverflow.com/questions/9352641/how-can-i-have-output-from-one-named-pipe-fed-back-into-another-named-pipe/9359882#9359882">A.H.'s answer</a> helped me understand that I wasn't using named pipes correctly.  I have since solved my problem by not even using named pipes.  That solution is here:  <a href="http://pastebin.com/VFLjZpC3" rel="nofollow noreferrer">http://pastebin.com/VFLjZpC3</a></p>