profile bashrc 실행 순서

(Profile bashrc bash profile 순서에서 넘어옴)

bash 쉘 초기화 파일 실행 순서
profile bashrc bash_profile 실행 순서
profile bashrc bash_profile 호출 순서

1 실행순서 (간단히 정리)[ | ]

기본 설정에서 실행 순서는 다음과 같다.

  • 각 파일의 맨 아랫부분에 실행 코드를 추가할 때의 기준이다.
  • 특별히 코드를 변경하지 않았다면 이 순서대로 실행될 것이다.
  • 실행코드를 하단에 추가하는 것이 보통이므로 이 정도만 알아두면 OK.
  1. /etc/profile.d/test.sh[1]
  2. /etc/profile
  3. /etc/bashrc
  4. ~/.bashrc
  5. ~/.bash_profile

2 호출 순서[ | ]

기본 설정에서 호출 순서는 다음과 같다.

  • 각 파일의 맨 윗부분에 실행 코드를 추가할 때의 기준이다.
  • 위의 문단과 혼동이 있을 수도 있으므로 대충 보고 넘어가자.
  1. /etc/profile → /etc/profile.d/test.sh
  2. ~/.bash_profile → ~/.bashrc → /etc/bashrc

3 코드분석[ | ]

~/.bash_profile에 다음과 같은 내용이 있다.

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

→ ~/.bashrc 가 있으면 그것을 실행시킨다.

~/.bashrc 에 다음과 같은 내용이 있다.

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

→ /etc/bashrc가 있으면 그것을 실행시킨다.

/etc/profile에는 다음과 같은 내용이 있다.

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "$PS1" ]; then
            . $i
        else
            . $i >/dev/null 2>&1
        fi
    fi
done

→ /etc/profile.d 폴더 내의 모든 sh 파일을 실행시킨다.

실행파일의 호출 순서를 정리하면 다음과 같다. (OS는 /etc/profile과 ~/.bash_profile 2개를 순서대로 호출할 뿐인데, 각 파일이 내부적으로 다른 파일을 호출하는 것이다.)

  • 리눅스 → /etc/profile → /etc/profile.d/test.sh
  • 리눅스 → ~/.bash_profile → ~/.bashrc → /etc/bashrc

이것을 각 파일의 마지막에 코드를 추가했을 때 기준으로 실행순서를 정리하면, 맨 위 문단에서 설명한 바와 같게 된다.

  • /etc/profile.d/test.sh → /etc/profile
  • /etc/bashrc → ~/.bashrc → ~/.bash_profile

4 실행순서 (정확히)[ | ]

각 파일에 아래와 같은 형식으로 명령어를 추가하여 추적해보았다.

NOW=`date +%Y-%m-%d\ %H:%M:%S`
echo "$NOW [/etc/profile.d/a.sh]" >> /root/run_order.txt

그 로그는 다음과 같다.

[root@zetawiki ~]# cat run_order.txt
2013-08-03 07:47:28 [START OF /etc/profile]
2013-08-03 07:47:28 [/etc/profile.d/a.sh]
2013-08-03 07:47:28 [/etc/profile.d/b.sh]
2013-08-03 07:47:28 [END OF /etc/profile]
2013-08-03 07:47:28 [START OF ~/.bash_profile]
2013-08-03 07:47:28 [START OF ~/.bashrc]
2013-08-03 07:47:28 [START OF /etc/bashrc]
2013-08-03 07:47:28 [END OF /etc/bashrc]
2013-08-03 07:47:28 [END OF ~/.bashrc]
2013-08-03 07:47:28 [END OF ~/.bash_profile]

5 같이 보기[ | ]

6 주석[ | ]

  1. 여기서는 test.sh라고 했지만 어떤 이름이든 상관없다. /etc/profile.d에 있는 모든 sh 파일이 여기에 해당된다.

7 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}