목적

  1. PATH 환경 변수에 값을 셋팅하는 두 가지 방법 알아보기
  2. 이를 통해서, PATH 환경 변수가 셋팅되는 과정을 알아보자 (zsh 기준)


Location 추가하는 두 가지 방법

  1. /etc/paths 파일 수정하기

    두 가지 방법

    1. paths 텍스트 파일을 수정한다
    2. paths.d 디렉토리에 들어가서 파일을 생성시키는 방식으로 path를 추가한다.
  2. 실행시킨 shell(bash, zsh)에 따라서 실행되는 ~/.bashrc, ~/.zshrc 을 수정한다.

    덮어쓰기 방식으로 추가한다.

    # ~/.zshrc
    export PATH=/Users/yunslee/Library/Python/3.8/bin:$PATH
    

환경 변수가 셋팅되는 과정 (zsh 기준)

  1. shell이 켜지면, zprofile이 실행된다.

  2. zprofile 의 쉘 스크립트 코드는 아래와 같다.

    Example : /etc/zprofile

    # System-wide profile for interactive zsh(1) login shells.
    # Setup user specific overrides for this in ~/.zprofile. See zshbuiltins(1)
    # and zshoptions(1) for more details.
    if [ -x /usr/libexec/path_helper ]; then
         eval `/usr/libexec/path_helper -s`
    fi
    
  3. /usr/libexec/path_helper 가 호출된다.

    1. /usr/libexec/path_helper bin 파일이다.

    2. man page를 읽어보자.

      • man path_helper

        스크린샷 2022-06-16 오후 1.16.20.png

        /etc/paths , /etc/paths.d 를 이용하여 환경변수 PATH를 설정해준다고 설명하고 잇다.

      • 터미널에 eval /usr/libexec/path_helper -s`` 로 실행시켜보면, 다음과 같은 결과 가 나온다.

        PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/yunslee/Library/Python/3.8/bin"; export PATH;
        
    3. /usr/libexec/path_helper에서 export 하여 PATH를 셋팅한다.

    결론

    Shell script가 내가 작성한 파일을 읽고, export로 PATH의 환경변수를 셋팅해준다.