0

I have bash1.sh file:

abc=100
export abc

Other file bash2.sh:

source bash1.sh

when I run ./bash2.sh and try to echo $abc or export -p , it doesn't display abc. But If I manually do source ./bash1.sh , it will print abc value. How to source ./bash1.sh from bash2.sh so abc will be global var?

  • It should work; there is probably something else going on. Are you running the script in the directory where both files exist? Do you get any sort of error message? – tripleee Dec 14 '20 at 09:08
  • 1
    Or do you mean you run `bash2.sh` and then try to `echo` this variable after it finishes? That won't work, for the same reason you have to `source` from inside the script for the variable to be available in the parent script. – tripleee Dec 14 '20 at 09:09
  • Directory is same for both files. [link](https://stackoverflow.com/questions/16618071/can-i-export-a-variable-to-the-environment-from-a-bash-script-without-sourcing-i) tried 3rd approach of first ans, not working for me. file prat1.sh : ```echo export FOO1=BAR``` and file prat2.sh: ```eval "$(./prat.sh)"```. Ran prat2.sh and ```$echo $FOO1```(cmd), nothing display. @tripleee – raghu01dvx Dec 14 '20 at 09:38
  • It's still not clear if you expect the variable to persist after `prat2.sh` finishes. If so, that can't be done. – tripleee Dec 14 '20 at 09:44
  • Precisely, I want process ID to be fetch from bash script and want to import in other file. I got PID in my bash script, exported it, when other file finds that variable value NULL. So how do i make that variable persist. Like "source " keeps manually. @tripleee – raghu01dvx Dec 14 '20 at 09:52
  • The duplicate really does explain this, and so do a number of similar questions. A process cannot change its parent's environment without cooperation from the parent. – tripleee Dec 14 '20 at 09:55
  • Alright. Thanks. @tripleee – raghu01dvx Dec 14 '20 at 09:57

0 Answers0