2

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.

  • 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
1

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)
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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