Interactive Shell과 Login Shell의 정의#
셸의 종류는 Interactive 여부와 Login 여부라는 두 가지 독립적인 기준으로 구분된다.
1. 기술적 정의#
1.1 Interactive Shell#
셸은 다음 조건 중 하나를 만족할 때 기술적으로 Interactive Shell(대화형 셸)로 간주된다.
- 옵션이 아닌 인자(단,
-s제외)와-c옵션 없이 실행되었으며, 표준 입력(stdin)과 표준 에러(stderr)가 모두 터미널에 연결된 경우(isatty(3)로 판단). 1 -i플래그와 함께 명시적으로 실행된 경우.
확인 방법:
[[ $- == *i* ]] && echo "Interactive" || echo "Non-interactive"[[ -n "$PS1" ]] && echo "Interactive" || echo "Non-interactive"
1.2 Login Shell#
셸은 오직 다음 조건 중 하나를 만족할 때만 기술적으로 Login Shell로 간주된다.
- 첫 번째 인자(
argv[0])의 시작 문자가 하이픈인 경우 (eg.-bash). 2 --login(또는-l) 플래그와 함께 명시적으로 실행된 경우.
확인 방법:
shopt login_shell(로그인 셸인 경우on반환)
ssh host <command>가 로그인 셸이 아닌 이유는 SSH가 셸을 실행할 때 bash -c "<command>" 형식을 사용하기 때문이다. 이 방식은 하이픈이나 로그인 플래그를 포함하지 않는다.
2. 셸의 조합#
Interactive 셸과 Login 셸 사이에는 엄격한 포함 관계가 없다.
| Login | Non-Login | |
|---|---|---|
| Interactive | Interactive Login Shell eg. SSH 로그인, 로컬 콘솔 로그인 | Interactive Non-Login Shell eg. 터미널에서 bash 실행 |
| Non-Interactive | Non-Interactive Login Shell eg. su - -c <cmd>3, bash --login -c <cmd> | Non-Interactive Non-Login Shell eg. bash -c <cmd>, su -c <cmd>, ssh host <cmd>4, ./script.sh, Crontab |
3. 설정 파일#
셸의 종류에 따라 실행되는 설정 파일이 다르다. 5
- Login Shell (Interactive 또는 Non-Interactive):
/etc/profile~/.bash_profile,~/.bash_login,~/.profile중 가장 먼저 발견되는 파일 하나만 실행.
Login Shell은
~/.bashrc를 자동으로 실행하지 않는다. 로그인 셸에서~/.bashrc가 실행되는 이유는 보통~/.profile이나~/.bash_profile내부에 이를 직접 불러오는(source) 코드가 포함되어 있기 때문이다.# if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi
- Non-Login Shell
- Interactive Non-Login Shell:
~/.bashrc - Non-Interactive Non-Login Shell: 보통 없음 (설정된 경우
$BASH_ENV읽음).
- Interactive Non-Login Shell:
3.1 $PATH 설정과 .profile#
$PATH 변수는 ~/.bashrc가 아닌 ~/.profile에서 설정하는 것이 권장된다.
- 상속 (Inheritance): 로그인 셸에서 (
~/.profile을 통해) 설정된 환경 변수는 모든 하위 프로세스(서브셸 포함)에 상속된다. 즉, 로그인 시 (~/.profile) 한 번 설정하는 것만으로 모든 서브셸에 적용된다. - 효율성 (Efficiency):
$PATH를~/.bashrc에 설정하면 서브셸이 실행될 때마다 중복해서 처리되므로 비효율적이며,$PATH가 불필요하게 중복되거나 길어질 수 있다.
References#
bash(1): “An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option, and whose standard input and standard error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.” ↩︎
bash(1): “A login shell is one whose first character of argument zero is a -, or one started with the –login option.” ↩︎
su(1): “Start the shell as a login shell with an environment similar to a real login.” The hyphen (
-) is a shortcut for--login. The-cflag makes the shell non-interactive as it executes a command instead of reading from a terminal. ↩︎bash(1): “Bash attempts to determine when it is being run by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run non-interactively in this fashion, it reads and executes commands from … ~/.bashrc.” When
bashis run non-interactively bysshd, it is not a login shell, but it specifically reads~/.bashrcanyway. ↩︎bash(1): “When bash is invoked as an interactive login shell… it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.” ↩︎