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
source test.sh
or. test.sh
. – doneal24 Dec 13 '22 at 23:13source <( ./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 atrap
function. – Paul_Pedant Dec 14 '22 at 10:16