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:
- Write the variable into a file
- 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.