Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a Main shell that runs a profile, The profile contains some variables such as Count. When I call the main shell, it loads the profile and then call another shell script. I want that inner shell script be able to access the variable count that the main shell script loaded already. How can I do that ? I tried export but only helped in one level variable import.

share|improve this question
    
you mean to use these variables as globals that can be changed by any sub shell, and have that change be recognized by all sub shells including the main shell so all shells will read the value updated by that child shell and be able to work on the updated value? –  Aquarius Power Sep 17 '13 at 23:24
add comment

2 Answers 2

export allows variables defined in a process, to be read in sub process, but if variable is modified in sub process, the variable will not change in caller process, because each process has it's own environment variables. Maybe a solution can be the sub shell to write to output and caller shell to read ouput.

count=$(subprocess)
share|improve this answer
add comment

Don't call the inner scripts as separate processes, call them as included scripts.

. /path/to/inner_script

Scripts read through the . (dot) built-in (also available under the name source in bash) are executed in the same shell environment as the caller. In particular, they share variables.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.