This is an attempt to simplify a problem that I am doing. I define a function that sets a variable and in this scenario it works:
$ function myfunc {res = "ABC"; $ Res = "XYZ" $ myfunc $ echo $ res ABC Therefore res myfunc has been replaced by calling but: $ res = "XYZ" $ myfunc | Echo $ Echo $ res XYZ So when myfunc is part of a pipe, the value does not change How do I work my way, the way I wish to join the pipes Do you also
(The actual script works more detailed work of "MiFunk" and a quick progression is on the other side of the pipeline rather than a useless echo)
thanks
This is not possible on UNIX. To understand this better, you have to know what a variable is, the bash maintains two internal tables with all defined variables, a current shell is for local variables, you have to call them set name = value Or just create with name = value . They are local to the process; When a new process is made, they are not inherited. To export a variable in new hair processes, you have to export it from export name , "I want kids to see the value of this variable ". This is a security feature. When you call a function in the bash, it is executed in the context of the current shell, so it can access and modify all the variables.
But there is a list of pipe processes that are attached to the I / O pipes. It means that your function is executed in shell and output of this shell only echo . Even in myfunc will not be exported as the export only works for the shells to be started by the shell where you exported and Echo was started by the same shell as myfunc : Bash + - myfunc + - echo < P> This is not a child of echo myfunc . Workaround:
- Write variables in a file
- Use more complex output formats such as XML or multiple lines where the first line of output is always variable And the real output comes in the next line.
Comments
Post a Comment