Search code examples
bashpipebash-function

Bash function fails when it's part of a pipe


This is an attempt to simplify a problem I'm having. I define a function which sets a variable and this works in this scenario:

$ function myfunc { res="ABC" ; }
$ res="XYZ"
$ myfunc
$ echo $res
    ABC

So res has been changed by the call to myfunc. But:

$ res="XYZ"
$ myfunc | echo
$ echo $res
    XYZ

So when myfunc is part of a pipe the value doesn't change. How can I make myfunc work the way I desire even when a pipe is involved?

(In the real script "myfunc" does something more elaborate of course and the other side of the pipe has a zenity progress dialogue rather than a pointless echo)

Thanks


Solution

  • This isn't possible on Unix. To understand this better, you need to know what a variable is. Bash keeps two internal tables with all defined variables. One is for variables local to the current shell. You can create those with set name=value or just name=value. These are local to the process; they are not inherited when a new process is created.

    To export a variable to new child processes, you must export it with export name. That tells bash "I want children to see the value of this variable". It's a security feature.

    When you invoke a function in bash, it's executed within the context of the current shell, so it can access and modify all the variables.

    But a pipe is a list of processes which are connected with I/O pipes. That means your function is executed in a shell and only the output of this shell is visible to echo.

    Even exporting in myfunc wouldn't work because export works only for processes started by the shell where you did the export and echo was started by the same shell as myfunc:

    bash
     +-- myfunc
     +-- echo
    

    that is echo is not a child of myfunc.

    Workarounds:

    1. Write the variable into a file
    2. Use a more complex output format like XML or several lines where the first line of output is always the variable and the real output comes in the next line.