"Profile bashrc 실행 순서"의 두 판 사이의 차이

잔글 (Jmnote님이 Profile bashrc bash profile 실행 순서 문서를 Profile bashrc 실행 순서 문서로 이동했습니다)
 
(사용자 2명의 중간 판 10개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{소문자}}
{{소문자}}
{{테스트|페도라 11 (Leonidas)}}
{{테스트|페도라 11 (Leonidas)}}
;bash 쉘 초기화 파일 실행 순서
;profile bashrc bash_profile 실행 순서
;profile bashrc bash_profile 실행 순서
;profile bashrc bash_profile 호출 순서
;profile bashrc bash_profile 호출 순서
25번째 줄: 26번째 줄:
==코드분석==
==코드분석==
~/.bash_profile에 다음과 같은 내용이 있다.
~/.bash_profile에 다음과 같은 내용이 있다.
<source lang='bash'>
<syntaxhighlight lang='bash'>
# Get the aliases and functions
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
if [ -f ~/.bashrc ]; then
         . ~/.bashrc
         . ~/.bashrc
fi
fi
</source>
</syntaxhighlight>
→ ~/.bashrc 가 있으면 그것을 실행시킨다.  
→ ~/.bashrc 가 있으면 그것을 실행시킨다.  


~/.bashrc 에 다음과 같은 내용이 있다.
~/.bashrc 에 다음과 같은 내용이 있다.
<source lang='bash'>
<syntaxhighlight lang='bash'>
# Source global definitions
# Source global definitions
if [ -f /etc/bashrc ]; then
if [ -f /etc/bashrc ]; then
         . /etc/bashrc
         . /etc/bashrc
fi
fi
</source>
</syntaxhighlight>
→ /etc/bashrc가 있으면 그것을 실행시킨다.
→ /etc/bashrc가 있으면 그것을 실행시킨다.


/etc/profile에는 다음과 같은 내용이 있다.
/etc/profile에는 다음과 같은 내용이 있다.
<source lang='bash'>
<syntaxhighlight lang='bash'>
for i in /etc/profile.d/*.sh ; do
for i in /etc/profile.d/*.sh ; do
     if [ -r "$i" ]; then
     if [ -r "$i" ]; then
53번째 줄: 54번째 줄:
     fi
     fi
done
done
</source>
</syntaxhighlight>
→ /etc/profile.d 폴더 내의 모든 sh 파일을 실행시킨다.
→ /etc/profile.d 폴더 내의 모든 sh 파일을 실행시킨다.


66번째 줄: 67번째 줄:
==실행순서 (정확히)==
==실행순서 (정확히)==
각 파일에 아래와 같은 형식으로 명령어를 추가하여 추적해보았다.
각 파일에 아래와 같은 형식으로 명령어를 추가하여 추적해보았다.
<source lang='bash'>
<syntaxhighlight lang='bash'>
NOW=`date +%Y-%m-%d\ %H:%M:%S`
NOW=`date +%Y-%m-%d\ %H:%M:%S`
echo "$NOW [/etc/profile.d/a.sh]" >> /root/run_order.txt
echo "$NOW [/etc/profile.d/a.sh]" >> /root/run_order.txt
</source>
</syntaxhighlight>


그 로그는 다음과 같다.
그 로그는 다음과 같다.
<source lang='dos'>
<syntaxhighlight lang='console'>
[root@jmnote ~]# cat 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 [START OF /etc/profile]
2013-08-03 07:47:28 [/etc/profile.d/a.sh]
2013-08-03 07:47:28 [/etc/profile.d/a.sh]
84번째 줄: 85번째 줄:
2013-08-03 07:47:28 [END OF ~/.bashrc]
2013-08-03 07:47:28 [END OF ~/.bashrc]
2013-08-03 07:47:28 [END OF ~/.bash_profile]
2013-08-03 07:47:28 [END OF ~/.bash_profile]
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
91번째 줄: 92번째 줄:
*[[~/.bashrc]]
*[[~/.bashrc]]
*[[~/.bash_profile]]
*[[~/.bash_profile]]
*[[Bash 쉘 초기화 파일]]


==주석==
==주석==
<references/>
<references/>


==참고 자료==
==참고==
*http://www.thegeekstuff.com/2008/10/execution-sequence-for-bash_profile-bashrc-bash_login-profile-and-bash_logout/
*http://www.thegeekstuff.com/2008/10/execution-sequence-for-bash_profile-bashrc-bash_login-profile-and-bash_logout/


[[분류: 리눅스]]
[[분류: 리눅스]]

2023년 5월 31일 (수) 14:11 기준 최신판

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 }}