2

I'm creating a bashscript to setup a small application I'm writing. Part of that setup requires setting an environment variable so that other processes may reference it.

My simple shell script test looks like this:

#!/bin/sh

export FOO=abc

I understand why FOO will not be visible after the script is executed, if done like so:

$ sh script.sh

and why this will make it visible:

$ source script.sh

so that's a workaround, but I'm wondering how common install scripts do this - for example, I'm pretty sure installers like mysql etc do set global environment variables.

What is the right way to do this? I guess I can ask my users to install by running:

$ source script.sh

but not sure if there is a better way.

Thanks

user3203425
  • 169
  • 3
  • 8

1 Answers1

1

Add these lines to your script. Your script should have root access.

echo 'export FOO=abc' > /etc/profile.d/foo.sh

Next restart it will be available.

dedunu
  • 9,186