Shell – Setting env variable through script on current session

environment-variablesshell-scriptsource

Is there a possible way of setting env variable through script on current session.

I have tried source option. But this works on the shell not through script.

here is my script content

source /etc/profile

I've added a new variable in the /etc/profile file before executing the script, but the variable doesn't show up in the current session.

Best Answer

It depends on how you call your script.

If you are calling the script in such a way that it executes in sub shell then the variable will not be visible in the current shell.

  1. try to execute the script in the current shell.

     #. ./yourscript
    
  2. You can also source the profile by this syntax in your script

     . /etc/profile
    

    a dot , followed by a space and then the full path

Related Question