-2

I was trying to run Maven (mvn) on Windows 10 in an ubuntu shell (WSL2) It complained that it cannot see the JDK.

$ mvn -archetype:generate -DgroupId=com.lynda -Darti
factId=sampleWeb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

So it got me wondering; there are a number of startup and login scripts. What is the best practice of where to put environment variables in?

.profile or .bashrc .bash_login

and at the beginning of the file, or at the end?

My question is about the best place to source the environment variables. This one is about the general difference between the files. It is not a duplicate of the other question.

likejudo
  • 3,396
  • 6
  • 52
  • 107
  • No, it does not answer my question as my question is asking about *where* to put environment variables. – likejudo Mar 04 '21 at 14:34
  • 1
    But that's what these files are all for. It depends on what you want the scope of your variable (or alias or function etc) to be. – tripleee Mar 04 '21 at 14:35
  • and that's why I mentioned specifics like JAVA_HOME – likejudo Mar 04 '21 at 14:37
  • Duplicates are useful for directing future visitors to a more detailed and comprehensive treatment of the subject; that doesn't mean that a quick and specific answer here could not be useful as well. But clearly you didn't think so; so I have deleted mine. – tripleee Mar 04 '21 at 15:03
  • Sorry, I think you misunderstood. I thought you were voting to close the question - there is one vote to close. I appreciated your specific answer as it was about the best place to source the environment variables. The other answer was about the general difference between the shells and I could not understand the answer to my question from reading that one. Please undelete your answer. – likejudo Mar 04 '21 at 15:24
  • For what it's worth, yes I did vote to close as duplicate. If you can explain how the duplicate is unclear, perhaps we could add a new answer to it. – tripleee Mar 04 '21 at 19:19
  • It is not a duplicate of the other question. – likejudo Mar 06 '21 at 13:11
  • The best way to do it was posted by user aran over here. https://stackoverflow.com/q/66507165/398348 – likejudo Mar 06 '21 at 17:29

1 Answers1

1

Generally probably .profile but this also depends on how your account is set up. Bash by default reads .profile on most platforms, but this file is also sourced by other shells, so it's useful for situations where you don't run a Bash login shell, too; but if you botch your Bash startup files, you can end up in a situation where Bash ignores this file (briefly, if you create .bash_profile and it didn't exist before, you should make sure it will source ~/.profile in turn).

It doesn't usually matter whether you add things at the beginning or the end, but the most common convention is to add things at the end. (When it does matter, adding at the end means you override something earlier in the file, where of course the opposite would usually be more problematic.)

Notice that export VARIABLE='value' might not be portable to legacy Bourne shell, though POSIX now supports this syntax. For maximum portability, you might prefer

VARIABLE='value'
export VARIABLE

on two separate lines, or with a semicolon between the statements.

.bashrc is for things which should be reinitialized every time you launch a new subshell; this is unnecessary for environment variables (and most other things people sometimes put there really) which only need to be set when you first log in, and then are inherited by all child processes of your login session, including any additional shell instances. (This is what the export keyword does.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • why not in `.bashrc`? – likejudo Mar 04 '21 at 14:35
  • Because that gets reloaded every time you start a new shell, but this variable typically only needs to be set once, and is then inherited by any shell you start after that. – tripleee Mar 04 '21 at 14:37
  • I added JAVA_HOME to .profile but when I did a `. ~/.profile` I found that JAVA_HOME did not get added. I thought that this will source the script again? I had to close everything out and then open a new Windows terminal again. – likejudo Mar 06 '21 at 13:13
  • It should have worked, yes. – tripleee Mar 06 '21 at 13:42
  • I think WSL2 is different. I posted a question here https://stackoverflow.com/questions/66507165/how-to-set-java-home-from-the-ubuntu-shell-on-wsl2 – likejudo Mar 06 '21 at 15:12