13

I'm try to export variables back to the parent process.

$ export VAR=FALSE
$ echo $VAR
FALSE

$ ./myprogram  # this will set VAR=TRUE
$ echo $VAR
TRUE  <========== I want to print `TRUE` here

2 Answers 2

13

Can't be done. The only reliable way to pass anything to the parent process is to echo it and have the parent process capture it with command substitution.

VAR=$(./myprogram)
5
  • I think some programs even have options to format their output accordingly.
    – Daniel Beck
    Dec 20, 2011 at 6:12
  • Depends what you mean with reliable. Named pipes are a pretty reliable way, without race conditions.
    – Pithikos
    Sep 4, 2014 at 17:14
  • The easiest way is to echo var=value couples in the child and call eval $(./myprogram).
    – user93692
    Jan 17, 2015 at 4:03
  • how do you do this with an array, e.g. echo "$@"?
    – jchook
    Mar 17, 2019 at 2:07
  • 1
    This is not the only way, answered here: unix.stackexchange.com/questions/38205/…
    – Andry
    Apr 8, 2019 at 13:24
5

Actually... I got this to work just now using source

If myprogram contains:

export VAR=TRUE

then after running source myprogram your case will work.

3
  • 3
    This should work even without export, but it should be explicitly stated it doesn't answer the literal question. source doesn't create a child, there is no child here, the original bash is not a parent process. Jul 17, 2018 at 8:51
  • 1
    You did not export variables to the parent process, you included them in the current process, as source works like "including a file". It could help some cases, but it's not a solution to the question "how to export variables back to the parent process". Sep 21, 2018 at 14:06
  • I'm with @KamilMaciorowski source doesn't create a child, there is no child here
    – Nam G VU
    Dec 30, 2020 at 6:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .