System.getenv does not get variables defined in ~ / .bash_profile

Here is the line in the ~ / .bash_profile file

export MESSAGE="Hello World"

I want to access the MESSAGE system variable in java.

System.getenv("MESSAGE"); does not work.

+6
source share
4 answers

The .bash_profile file is available only for login systems. If your Java process is created from a shell that is not an input shell (for example, the script with #!/bin/sh at the top), it will not read it (although it can still inherit the MESSAGE from the environment depending on how you run it).

Please also note that .bash_profile also does not start for interactive shells that are not โ€œloginโ€ shells, so you cannot rely on it to run even if you have a shell prompt. For this purpose, they usually use .bashrc, which is used for all interactive shells.

If you want the variable to be set in all derivatives of the Bourne shell, whether they are interactive or not, put them in both .profile and .bashrc.

+3
source

I would recommend you check if your environment variable is really โ€œdefinedโ€ using echo $ MESSAGE, as suggested above.
Also, modifying the bash_profile file will not affect your current shell,
you must use the "source" so that it can use your current shell.
I would also recommend reading this article about the differences between bashrc and bash_profile.
Maybe you should define EXPORT in bashrc?

+1
source

It really becomes even more interesting for users who have .profile (for the old Bourne shell), which is automatically read with .bash_profile (ensuring compatibility). In any case, environment variables are read only once when the login shell is launched, and all sub-object objects inherit these variables for free .. bashrc for tty-dependent things and unlucky functions (the old sh used $ ENV, I think if it were set, for something like that).

Your use of ~ / .bash_profile looks great (although single quotes are more reliable than double quotes, which allow some replacements). Make sure you are logged out and back between editing this file and trying to test it, or use ". ~ / .Bash_profile" (there are no quotes and note the leading dot and space, since dot is the command here).

The article http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html describes some good things, like using ". ~ / .Bashrc" at the end of your ~ /. bash_profile (excepth which you should use -r, not -f). The comment about using export in your .bashrc is incorrect, you should not do this for two reasons (1) a rather insignificant performance limitation, (2) a rather high probability that some command that you execute will not receive an environment variable - in particular, things arising from the window manager menu and other places where the actual command prompt did not appear in any of their parents.

Finally, make sure that $ MESSAGE really exists in your environment - look at the output of the env command - if it is not there, the Java process will not see it if it does not create it internally and does not save it in its internal copy of the environment variables.

Another note: if you set env variables to .profile, use this syntax:

VAR = VALUE; export var

... since the old sh shell does not support "export VAR = VALUE". Using set -e before a bunch of them and set + e after, you can remove the need to use "export" in general, if I remember correctly.

0
source

Another place to view: / etc / environment (this can override / replace your .bashrc or .bash_profile in shells open through your IDE)

0
source

Source: https://habr.com/ru/post/924765/


All Articles