Question

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

Était-ce utile?

La 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.

Autres conseils

As @Aaron said, the problem is caused by the function running in a subshell. But there is a way to avoid this in bash, using process substitution instead of a pipe:

myfunc > >(echo)

This does much the same thing a pipe would, but myfunc runs in the main shell rather than a subprocess. Note that this is a bash-only feature, and you must use #!/bin/bash as your shebang for this to work.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top