terminal
을 사용해서 처음에 bash
를 할당 받기 위해서는 로그인 절차를 지나게 된다.
그리고 로그인에 성공하면 bash
프로그램이 실행되고 prompt
가 화면에 나온다.
그런데 사실 우리가 로그인하는 순간 linux
에서는 shell
을 위한 몇가지 설정을 해준다.
그리고 이 세팅을 할 때 쓰는 파일이 있는데, 그게 바로 start up file
이다.
start up file
에는 크게 3가지 종류가 있다.
login shell
설정 (사용자 공통)login shell
설정 (사용자 개별)Non-Login shell
설정/etc/profile
파일이 바로 모든 사용자 공통 start up file
이다.
내용은 아래와 같다.
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
## prompt의 출력 방식을 지정한다.
if [ "${PS1-}" ]; then
if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# ' # uid 가 0인 것은 root 계정을 의미한다. 프롬프트에 # 를 쓴다.
else
PS1='$ ' # 일반적인 사용자라면 프롬프트에 "$"를 쓴다.
fi
fi
fi
###################################### 중요 ######################################
# 이 아래 있는 statement 가 핵심이다.
# 조건문 "if [ -d /etc/profile.d ]; then" 를 통해서 /etc/profile.d 디렉토리 여부 검토한다.
# 만약에 있다면, for 문을 돌면서 /etc/profile.d/ 디렉토리 내에 있는 모든 *.sh 를 실행한다.
# 다음 조건문인 "if [ -r $i ]; then" 을 통해 "파일이 존재하고 읽을 수 있는지" 검토한다.
# 읽을 수 있는 파일이면, 실행(". $i")!한다.
#
# 그렇다면 우리가 모든 사용자 공통 설정을 하려면 어떻게 해야될까?
# /etc/profile.d 디렉토리에 적절한 *.sh 파일을 생성하면 끝인 것이다!
# 생성만 해두면 linux가 알아서 실행해준다.
#################################################################################
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
###################################### 참고 ######################################
# 참고로 스크립트 내에서는 . 을 사용한 실행 방식을 많이 쓴다.
# 이러면 새로운 bash 프로세스를 생성하지 않고,
# 현재 보고있는 스크립트를 실행하는 bash 위에서 작동한다.
#################################################################################
~/.bash_profile
~/.bash_login
~/.profile
순서대로 우선순위가 잡히며, 이 파일들이 다 3개 다 있으면 1개만 실행된다.
예를 들어서...
사용자 자신의 홈 디렉토리에
.bash_profile
,.bash_login
,.profile
파일이
있다면.bash_profile
1개만start up file
로 이용된다.
그런데 우리가 linux 에서 사용자와 홈 디렉토리 생성하면 ~/.profile
이 자동 생성된다.
즉 우리는 사용자를 만들기만 해도 개별 사용자 설정파일이 생기는 것이다.
내용은 아래와 같다.
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
# umask 022 #### 기본 umask 값을 변경할 수도 있다.
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc" ################### 1번
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then ################### 2번
PATH="$HOME/bin:$PATH"
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
1번
주석 옆을 보면 알겠지만 ~/.bashrc
를 실행한다.
나중에 보겠지만 이 start up file
은 Non-Login shell
을 위한 것이다.
하지만 그와 별개로 그냥 실행하는 것을 볼 수 있다.
2번
을 보면 홈 디렉토리에 bin
이라는 디렉토리가 있으면, 해당 디렉토리를
PATH 환경 변수에 추가하라는 것도 보인다.
한마디로 우리가 홈디렉토리에 bin
을 생성하고 안에 good.sh
파일을 생성하면
이후에 어느 working directory
에서든 good
을 입력해서 good.sh
를 실행할 수 있다.
~/.bashrc
가 로그인 안 하고 bash
를 쓰게되면 사용되는 start up file
이다.
참고: 로그인 안하고
bash
를 쓰는 방법은?
로그인 상태에서bash
를 입력하면 새로운 쉘이 생성된다.
이때 생성된bash
가 바로Non-Login shell
이다.[dailyCode@myComp:~]$ bash [dailyCode@myComp:~]$ ps -f UID PID PPID C STIME TTY TIME CMD dailyCode 2749 2745 0 09:09 pts/0 00:00:00 -bash dailyCode 3027 2749 0 10:17 pts/0 00:00:00 bash # non-login shell dailyCode 3033 3027 0 10:17 pts/0 00:00:00 ps -f
ps -f
를 통해서CMD
에서bash
앞에-
의 여부에 따른 구별이 가능하다.