Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 3
    A co-process as its name suggests runs in a different process, so whatever modifications you make to variables in that other process won't affect the variable of the main shell process, you'd need to define a protocol for transmitting that variable (co-processes come two pipes one for input one for output for communication). If you want a variable that is incremented every second, look at the $SECONDS variable. Commented Jul 28, 2020 at 12:56
  • I thought "linking" with a name reference variable could allow me to get a connection to the different process. I know it´s possible to communicate using FDs, but I would need to use echo all the time which means the FD would have many entries than just one single variable. Commented Jul 28, 2020 at 13:17
  • By protocol, I meant you'd need to send something like give-me-a-number to your coproc and read the result back. Shell coprocs are not a very useful feature in general, see How do you use the command coproc in various shells? about that. Commented Jul 28, 2020 at 13:31
  • I am not sure if I correctly understand you, but I guess you mean I should perfom some kind of read to always have the latest output saved? My initial idea was using the local -n variable for reading the result back (which under "normal" conditions works just fine). I don´t need to input any number to it. I want to to run always in the background and be able to use the latest result of my background process. Commented Jul 28, 2020 at 13:50
  • 2
    A co-process is a different process, you won't get around that. It's intended to run things like servers that take input from the shell and send output back to it. The only communication channels between the processes are those two file descriptors for input/output, there is no magic sharing of variable values between the processes. That coproc shell process will have its capture variable, your main shell process will have its own with no relation between the two other than the coproc's one will have initially been copied from the main shell upon forking. Commented Jul 28, 2020 at 14:41