OS/Linux

[RedHat, CentOS] shell에서 환경변수 설정이 적용되지 않는 문제 해결 방법(do not work source command in shell script)

노루아부지 2023. 5. 21. 12:34

RHEL 7.9에서 java 자동 설치 shell을 만들던 중 java build path 지정을 위해. bash_profile에 java의 path를 추가 한 뒤 프로파일 적용 명령어인 source를 실행했는데 적용되지 않았습니다.

 

다음은 제가 만들었던 shell 코드입니다.

echo "" >> $HOME/.bash_profile
echo "export JAVA_HOME=/PrintChaserServer/jdk" >> $HOME/.bash_profile
echo "export JRE_HOME=/PrintChaserServer/jdk/jre" >> $HOME/.bash_profile
echo "export PATH=$PATH:/PrintChaserServer/jdk/bin:/PrintChaserServer/jdk/jre/bin" >> $HOME/.bash_profile
echo "export CLASSPATH=$CLASSPATH:/PrintChaserServer/jdk/lib/tools.jar" >> $HOME/.bash_profile
echo "" >> $HOME/.bash_profile
source $HOME/.bash_profile

 

원인

  • shell script file 실행 시 실행의 주체가 되는 shell process가 새로운 sub shell process를 생성하여 sub shell 내에서 실행함
  • sub shell은 부모쉘과 독립된 메모리를 사용하기 때문에 이 때 등록한 환경변수를 script를 모두 실행하고 부모쉘로 돌아갔을 때 사용할 수 없음

 

해결방법

  • source 명령어를 사용하여 스크립트 파일을 실행

 

주의사항

  • 현재의 shell에서 실행되기 때문에 script 내에 exit 명령어가 동작하면 세션이 종료됨 (창 닫힘)
  • cd(경로이동)을 했다면 마지막 이동한 경로에서 종료되기 때문에 시작 경로로 다시 이동한 후 종료해야 함

 

[참고 사이트]

https://kimyhcj.tistory.com/432

 

do not work source command in shell script

.bash_profile 을 이용하여 LD_LIBRARY_PATH 에 필요한 Path를 추가하고 System 을 on/off 하지 않더라도 바로 적용해야 할 경우 command 로 source 를 이용한다. source ~/.bash_profile 또는 이렇게 . ~/.bash_profile install sh

kimyhcj.tistory.com

 

728x90
loading