Bash: outputting a pipe to a background process?

I would like to put a process in the background and then pass data to it multiple times. For example:

cat &                    # The command I want to write into
cat_pid=$!               # Getting the process id of the cat process

echo hello | $cat_pid    # This line won't work, but shows what I want to
                         #   do: write into the stdin of the cat process

      

So I have a PID, how can I write to this process? I would be open to starting the cat process in a different way.

Also, I am on a Mac so I cannot use /proc

:(

+3


source to share


2 answers


First create a channel:

$ mkfifo pipe

      

Second, start your feline process with input from the pipe:



$ cat <pipe &
[1] 4997

      

Now send data to the channel:

$ echo "this is a test" >pipe
$ this is a test

      

+3


source


mkfifo .pipe
cat < .pipe &
echo hello > .pipe

      



+2


source







All Articles