Saturday, April 27, 2024
33
rated 0 times [  33] [ 0]  / answers: 1 / hits: 122511  / 2 Years ago, mon, august 22, 2022, 5:44:53

I'm using zsh as my shell, and I'm trying to configure my environment.



I usually define my $JAVA_HOME variable by creating a file:



/etc/profile.d/java.sh


with the following content



export JAVA_HOME=/path/to/jdk
export PATH=$JAVA_HOME/bin:$PATH


then I logout and back in, and it all works, but for some reason the PATH variable is not set. It recognizes JAVA_HOME, but not the new PATH, see this terminal snippet:



~  echo $JAVA_HOME
/usr/lib/jvm/jdk1.8.0_05
~ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games


and I confirmed it by trying to run a command form the jvm



~  java -version
zsh: command not found: java


the PATH doesn't include the $JAVA_HOME as it should. is there something else I should check?



I have checked that if I run:



source /etc/profile.d/java.sh


it all runs correctly and my variables get set as they should, but shouldn't the scripts in /etc/profile.d run automatically?


More From » environment-variables

 Answers
3

From my point of view, the best way is to add the following lines at the ~/.zshrc file (if you don't already have it, then create it):



if [ -d "/path/to/jdk" ] ; then
export PATH="/path/to/jdk/bin:$PATH"
fi


Then restart your zsh, or just run source ~/.zshrc and then your PATH should be exactly as you wish.



Or, if you want to make the change to be system-wide, then add the previous code to the end of /etc/zsh/zshenv file.



But in any case do not use /etc/profile.d to automatically run scripts in zsh. This directory is useful only for the bash shell, not zsh as in your case. To understand this, open /etc/profile file, which is a bash initialization file and in no case a zsh initialization file, and you will see somewhere at the end of the file:



if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi


So, your scripts from /etc/profile.d directory will automatically run in zsh only if you add the previous code in a zsh initialization file, like /etc/zsh/zprofile for example, or source /etc/profile in /etc/zsh/zprofile file.


[#24832] Tuesday, August 23, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rstride

Total Points: 305
Total Questions: 112
Total Answers: 118

Location: Mali
Member since Sat, Dec 26, 2020
3 Years ago
;