I am currently looking for a way to set enviroment variables in Linux via a simple shell script. Within the script I am currently using the ‘export’ command, however this only has scope within the script where system-wide scope is needed.
Is there anyway I can do this via a shell script, or will another method need to be used?
When you run a shell script, it executes in a sub-shell. What you need is to execute it in the context of the current shell, by sourcing it with:
or:
The latter is my preferred approach since I’m inherently lazy.
If you’re talking about system-wide scope inasmuch as you want to affect everybody, you’ll need to put your commands in a place where they’re sourced at login time (or shell creation time),
/etc/profilefor example. Where you put your commands depends on the shell being used.You can find out what scripts get executed by examining the
manpage for your shell:The
bashshell, when invoked as a login shell (including as a non-login shell but with the--loginparameter), will use/etc/profileand the first of~/.bash_profile,~/.bash_loginor~/.profile.Non-login
bashshells will use. unless invoked with--norcor--rcfile <filename>, the files/etc/bash.bashrcand~/.bashrc.I’m pretty certain it’s even more convoluted than that depending on how the shell is run, but that’s as far as my memory stretches. The
manpage should detail it all.