Normally you can set global variables inside bash functions.
But it seems not to be possible if the function is called by the pipe command. Is it possible to change this?
function myfunc()
{
MYVAR=123
}
echo "hi" | myfunc
echo "MYVAR is $MYVAR" # $MYVAR is not set. But why?
myfunc
echo "MYVAR is $MYVAR" # $MYVAR is now set as expected
Edit:
@Maroun wrote that piped functions are executed in a seperate process. But that cannot be as the following example shows:
#!/bin/bash
echo "main process is $$"
my_func ()
{
echo "my_func is executed in process $$"
}
echo "hi" | my_func
Output:
main process is 3225
my_func is executed in process 3225
As you can see the process id is the same. Any ideas?