I need to set some variables such as JAVA_HOME and add them to the PATH. Can I somehow define them in .profile, and have them pulled into .bashrc? Or maybe add them to a 3rd file, and have that file referenced in .bashrc and .profile?
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable. It will not do this if invoked as sh. The --norc option may be used to inhibit this behavior, and the --rcfile option may be used to force another file to be read, but rshd does not generally invoke the shell with those options or allow them to be specified.
In other words,
~/.bashrc will normally both be read when you start
bash, so you can put it in either.
You can also use the
source luke in either file to load the file
luke where you would define all your Javan variables. See
help . or
help source for details.
First, You will never add JAVA_HOME to your PATH. They serve different purposes as environment variables. The first, sets where your java binaries are installed so other java based programs can know. The second, is a set of places where bash will look for commands you type in terminal, like
Second, both the files will work, but you have to be careful when setting variables inside .bashrc, because you may overwrite the ones already configured in other files, as PATH. The most safe way of doing this is by adding ":$PATH" at the end of the path. It would be something like this (adds java binaries to your PATH to be able to call commands such as
To set JAVA_PATH, the most safe (and automatic... rsrs) way to do so is by adding the following code in the end of .bashrc:
export JAVA_HOME=$(update-alternatives --query javac | sed -n -e 's/Best: *\(.*\)\/bin\/javac/\1/p')
After you have done so, you will have do execute
exec bash, or close and reopen the terminal window. To check if your variables are set, run on the terminal:
echo $JAVA_HOME echo $PATH
You should be able to see the value inside the variables.
Remember that all these files like .bashrc are located in your home directory. Something like
/home/<your name> or just by typing
cd without any other arguments. Remember too, that files that start with a 'dot'
. are not visible just by typing
ls on terminal. if you want to see these files you have to
You can do exactly what you mentioned. When you remote ssh into bash terminal the .bash_profile will be sourced and when you create a new xterm the .bashrc will be sourced. Would be better if you put your environment variable information into another file .profile and from both of the .bash_profile and .bashrc source the .profile