0

I have two scripts that call each other. I need to modify a given variable in both. It doesn't work.

A.sh :

#! /bin/sh
funcA() {
    echo "var B in funcA (1) $__varB"
    __varB="xxx"
    echo "var B in funcA (2) $__varB"
}
. B.sh

B.sh :

! /bin/sh
__varB="asdf"
funcA | tee -a out.txt 2>&1 #if no pipe then it works
echo "var B in B.sh $__varB"

Execution

./A.sh
var B in funcA (1) asdf
var B in funcA (2) xxx
var B in B.sh asdf

If in B I do not pipe funcA into tee then it works as expected and __varB is modified. My problem is that in the real case I can't modify B.sh.

What can I do in A.sh so that I modify __varB ?

1 Answer 1

1

The redirected function is apparently run in a subshell. To avoid it, you have use the process substitution:

funcA > >( tee -a out.txt 2>&1 )

This change must appear in B.sh. If you cannot modify it, you are doomed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.