How to pass values to a script called from within another script in shell?

Need ideas on how to achieve the below.

We have a script say "profile.sh" which internally calls another existing script called "name.sh" which prompts for the
name and age of a person upon execution. When i run profile.sh how can i populate a pre-defined value from another file and pass that to "name.sh" for the values of name and age.

OS: RHEL 6.0

Usually it's best to show what you've tried and where you failed, so people know where to start guiding / helping you. On top, describe your request more detailedly. E.g. what do you mean by "populate a pre-defined value from another file"? Pls explain in depth!

Just executing a script from within another script will do so in a subshell, which can inherit variables exported by the caller, but there's no such channel back. There are several ways to get some info back to the caller, though (for the bash shell, not necessarily available in others):

  • use "command substitution" to intercept the second script's stdout and e.g. assign it to a (one!) variable.
  • source the script, so no subshell will be created / used.
  • read from a "here string" which in turn uses "command substitution".
  • go through a file / FIFO.
  • a coprocess might work for certain situations.
2 Likes