0

It's possible to define/declare a global/persistent variable in the /etc/profile file like this:

export my_variable=value

I can use this variable in a shell:

echo $my_variable

But can I also define it by using a script? I want that the modification shall be visible for any other running scripts. And the new value shall of course be available in case of reboot.

Could setenv be the solution? I don't know because this function isn't available in my linux (variant of openwrt).

I tried export my_variable=new_value in a script. $my_variable gives correctly the new value in the script. But in another script my_variable has the previous value: the persistent variable didn't change!

I tried echo "export my_variable=new_value" >> /etc/profile. Same result!

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Bertaud
  • 2,888
  • 5
  • 35
  • 48
  • 1
    Possible duplicate of [Can I export a variable to the environment from a bash script without sourcing it?](https://stackoverflow.com/q/16618071/608639), [Can a shell script set environment variables of the calling shell?](https://stackoverflow.com/q/496702/608639), [Set a parent shell's variable from a subshell](https://stackoverflow.com/q/15541321/608639), etc. – jww Oct 29 '19 at 11:14
  • Sorry but I don't see the solution of my questions in your propositions – Bertaud Oct 29 '19 at 11:59
  • And no notion of parent in my question. No duplicate or probe it – Bertaud Oct 29 '19 at 12:05
  • 1
    The only way you can modify the environment of another process is pass a value from the parent to a child *on startup*. Once the child's environment is initialized, you can't modify it further from outside that process. Children cannot affect parents, parents cannot further affect children, and in general, one process cannot affect another. – chepner Oct 29 '19 at 13:13
  • Environment variables are not global variables. – chepner Oct 29 '19 at 13:14
  • Ok, I mean Global, not Environment. Thank you for the correction.I don't want one process is the child. I want affect the other processes with Global variable – Bertaud Oct 29 '19 at 15:03
  • The traditional way to manage a variable that needs to be visible by multiple processes (and allows for persisting modifications) is to store value in a file that is sourced (and rewriteable by other scripts/processes). So while `/etc/profile` is one place to store that value, that is a high risk file to be modifying. You can have a line in `/etc/profile` like `source /path/to/special/variables/set_my_variable` and then, assuming correct permissions, you and any script/program can modify the `export var=value` in `set_my_variable` as needed. But watch out for contension between scripts. GdLuck – shellter Oct 29 '19 at 16:54
  • And, I gnerally, I wouldn't even "pollute" `/etc/profile` with such a specific reference. Scripts often have their own enviroment file that is sourced at the top. This could then source `/path/to/special/variables/set_my_variable, reducing the scope to only programs that really need this information. Good luck. – shellter Oct 29 '19 at 16:58
  • I didn't understand very well (I am not a Linux expert). In /etc/profile: export my_variable="abc". In my_script.sh: source /etc/profile echo $my_variable $my_variable="def" echo $my_variable. In another script: source /etc/profile echo $my_variable. In this case my_variable should be "def": correct ? – Bertaud Oct 30 '19 at 08:43
  • Forget my previous comment. In /etc/profile: source myfile. In myfile: export myvar=value. In myscript: myvar=newvalue >> myfile – Bertaud Oct 30 '19 at 09:32

0 Answers0