0

Is it possible to export env vars to the current shell, from within a child process/script that was executed from the current shell?

Take this ultra-simplified example script:

#!/bin/bash

export FOO="bar"

echo "The value of FOO is '$FOO', however it won't be in your env vars"

As expected, the var is not set after the script finishes:

$ ./test.sh
The value of FOO is 'bar', however it won't be in your env vars

$ echo $FOO

$

Is there any way to make this work so that the var is set after the script completes?


A bit of info on my use case:

  • The real world situation generates dynamic values based on multiple conditions which is why it's being set through a script rather than adding it to the users environment statically.
  • Right now, as a workaround, I'm just echoing the export commands to the user so they can paste it to their terminal window
Rino Bino
  • 121
  • 2
    You will need to source the script instead of executing it. Try source test.sh or . test.sh. – doneal24 Dec 13 '22 at 23:13
  • If you do not want to source the whole child process script, you can just source the export statements. The child sends these (and nothing else) to stdout, and the parent reads them with source <( ./ExChild ). Or use a /tmp file. The main point is that the parent has to ask for the values: they cannot be thrust into it. You could probably even make this async -- have the child send SIGUSR2 to its parent, and the parent reads the latest data in a trap function. – Paul_Pedant Dec 14 '22 at 10:16

0 Answers0