Search code examples
bashvariablesexportglobal-variables

Save Global variables BASH


I am new at bash and trying to solve some issues for a code I'm trying to make.

I am at the terminal under my user name and connect to bash

USER$  
USER$ bash  
bash$

now in the bash I am saving some variables f.e:

i=2   
k=2   
let p=$k*$i

now I want to use those variables outside the bash function

bash$exit   
USER$

but now the variables are not there

I try using export, but it did not really work, could use ur help, tnx


Solution

  • Unlike a DOS batch file, a Unix shell script cannot directly affect the environment of its calling shell.

    You could consider using the . (dot) or source command to read and execute the script in the context of the calling shell. This means that changes made in the script do affect the environment (in general; you can still run into issues with sub-shells).

    The other alternative is to have the script that sets the variables write the values in name=value format into a file which the calling script then reads (with . or source again).