How to programmatically set a permanent environment variable in Linux?
LSB-compliant (see spec) practice is to create a shell script in /etc/profile.d/
folder.
Name it after your application (and make sure that the name is unique), make sure that the name ends with .sh
(you might want to add scripts for other shells as well) and export
the variables you need in the script. All *.sh
scripts from that directory are read at user login--the same time /etc/profile
is source
d.
Note that this is not enforced by bash
; rather, it's an agreement of sorts.
Set Linux environment variable programmatically in Java
Environment variables, when set, are only available to processes spawned by the process where the variable is set, so if you're really asking to set a variable that will affect the entire system, then no. You can't really do that at all in Linux interactively. The best you could do is alter one of the system startup files to include said variable so that anything you do in the future would include that variable.
If you're simply looking to run several processes with some variable set, then ProcessBuilder allows you to set an environment for processes spawned with it. Reusing an environment for several processes is pretty trivial with that.
permanently set and edit environment variable in centos linux
Those commands do not modify any files - they read a file if it exists, then exports a variable in the current context only, and prints the value to standard output (usually the terminal).
Modifying a dotfile programmatically is not recommended, because there are a host of subtle bugs you can encounter both because of the mix of actual code with printed code (in the modifying script) and because you essentially can't be sure that the target file contains something which makes your script behave unexpectedly. This, coupled with the danger of not being able to log in as your normal user, are good reasons to avoid this in general. You have been warned.
To actually do this, you could simply add a new line with the new value of the variable at the end of the file:
echo "USER_PROMPT=haloooo" >> "$VARIABLE_FILE"
This will result in duplicate entries, but if you are just putting text and variables in the variable definition it should at least be fast, and pretty safe as long as your file always ends with a newline.
A more advanced version would be to replace the line if it exists:
if grep '^USER_PROMPT=' -- "$VARIABLE_FILE"
then
sed -i -e 's/^USER_PROMPT=.*/USER_PROMPT=haloooo/' -- "$VARIABLE_FILE"
else
echo "USER_PROMPT=haloooo" >> "$VARIABLE_FILE"
fi
Be warned that if you want to put arbitrary text in the replacement value, you have to make sure to escape it for sed
. Also, this command will overwrite every line which starts with USER_PROMPT=
, so if you want it to have different values at different times during your script (variable reuse is generally evil) you just introduced a bug. If you need that, you could try replacing only the last occurrence.
More advanced versions might be able to handle indented code, checking whether code is part of a string or a comment, and other features, but in the end, to do this reliably you would have to parse the language structure, which in Bash is not easy.
making environment variable permanent
Add it to your ~/.bashrc
and ~/.bash_profile
- one of these will be read when bash starts. .bash_profile
is read when you login, .bashrc
is read when you create a new interactive non-login shell (such as opening a terminal window from X)
How to permanently export a variable in Linux?
You can add it to your shell configuration file, e.g. $HOME/.bashrc
or more globally in /etc/environment
.
After adding these lines the changes won't reflect instantly in GUI based system's you have to exit the terminal or create a new one and in server logout the session and login to reflect these changes.
Set environment variables in C
I'm going to make a wild guess here, but the normal reason that these functions appear to not work is not because they don't work, but because the user doesn't really understand how environment variables work. For example, if I have this program:
int main(int argc, char **argv)
{
putenv("SomeVariable=SomeValue");
return 0;
}
And then I run it from the shell, it won't modify the shell's environment - there's no way for a child process to do that. That's why the shell commands that modify the environment are builtins, and why you need to source
a script that contains variable settings you want to add to your shell, rather than simply running it.
Related Topics
How to Change Port Number for Jenkins Installation in Ubuntu 12.04
Add Column to End of CSV File Using 'Awk' in Bash Script
Systemd: "Environment" Directive to Set Path
Grep String Inside Double Quotes
Change System Date Time in Docker Containers Without Impacting Host
Simulate Network Latency on Specific Port Using Tc
How to Specify Time in Cron Considering Year
Extract Date from a File Name in Unix Using Shell Scripting
"Tput: No Value for $Term and No -T Specified " Error Logged by Cron Process
How to Do Versioning of Shared Library
How to Read the Second-To-Last Line in a File Using Bash
Dma Cache Coherence Management
Determine the Os Version, Linux and Windows from Powershell
How to Calculate the Total Size of Certain Files Only, Recursive, in Linux
How to Read the Last Line of a Text File into a Variable Using Bash
Find Command in Bash Script Resulting in "No Such File or Directory" Error Only for Directories
Bash Scripting - Read Single Keystroke Including Special Keys Enter and Space