Set a Global Variable

I'm finding it difficult to solve this.

I want to set a variable that is shared between ALL the processes within the system.

export VAR = value

thus I make the variable visible inside the shell and its children

export VAR = value >> /root/.profile

so I make the variable visible to all the new shells I open remotely

if I start a script remotely, or at system startup, or by pressing a button on a webgui, or through a magic spell, I want that script or any other script to use the value contained in the variable.

looking on the net many people talk about "sharing" of memory, but it seems to me a cumbersome solution, I don't know if it's the right one and I don't find exhaustive documentation.

Is there a simple solution? Something I don't know (because I'm a noob) that allows me to solve by modifying a file or using a particular program?

I have been trying to resolve it for at least four hours, please help me

1 Like

have init do it... or put it on the cmdline... where anything can get to it....

Depending on the earliest possible time you will need to use that variable after the router boots up, you can potentially have the declaration in /etc/rc.local.

Given that you've had 2 errors in the 2 examples you've posted, I have to say these:

  1. Put the declaration above the exit 0 line of /etc/rc.local
  2. No spaces around = are allowed
  3. After you modify the /etc/rc.local make sure to run ash -n /etc/rc.local to verify it parses OK
  4. After that reboot the router, connect to it over ssh and try echo $VAR
2 Likes

I used:
"source"

https://bash.cyberciti.biz/guide/Source_command

which executes the script and takes the variables.

it is not a solution but only a patch.

I will try your solution that allows me not to have to go every time on all the scripts to insert the string with the "source" ...

I apologize for the syntax error.

Thank you so much for your support!

Some would consider that good practice.

1 Like

today i'm making a clean installation...
and now...
my script cannnot use source with this version...
I dont know what is missing and what i should install...
I'm going crazy...

More details please.

i use a new clean version of openwrt (working on glinet mt300nv2)

i think some package is missing

using source from shell make no problem, but my script do not load the variables

Likely a problem with your scripts then, as it seems you've confirmed that sh is behaving as expected.

sh -vx

is a common way to debug shell scripts. Another is using using echo or printf to output values at various points in the script's execution.

As a wild guess, you're not providing a fully qualified path to the script that gets source-d.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.