Zsh
Zsh는 macOS의 기본 셸입니다. 매우 사용자 정의가 가능하며 Bash와 호환됩니다.
사용 시기
- 고생산성 대화형 셸: 대화형 자동 완성, 퍼지 탐색, 기록 검색, 브랜치 표시기.
- 현대적인 CLI 플러그인 생태계:
zsh-autosuggestions,zsh-syntax-highlighting, Starship 프롬프트로 셸 워크플로우 가속화. - 강력한 셸 스크립팅: 배열 조작과 글로빙을 사용하여 POSIX 호환 및 Zsh 확장 자동화 스크립트 작성.
- 엔터프라이즈 환경 관리: macOS 및 Linux 서버에서 SDK 버전, PATH 내보내기, 별칭 관리.
빠른 시작
1. 최소 ~/.zshrc 설정
# History configuration
HISTFILE="$HOME/.zsh_history"
HISTSIZE=50000
SAVEHIST=50000
setopt HIST_IGNORE_DUPS
setopt SHARE_HISTORY
# Key bindings (emacs mode)
bindkey -e
# Autocompletion
autoload -Uz compinit && compinit
# Aliases
alias ll="ls -lah"
alias g="git"
alias gs="git status -sb"
2. 시작 시간 측정
# Profile zsh startup duration
time zsh -i -c exit
핵심 개념
고속 최신 Zshrc (~/.zshrc)
20ms 미만의 셸 시작, 스마트 완성, 기록 구성:
# ~/.zshrc
# History configuration
HISTFILE="$HOME/.zsh_history"
HISTSIZE=50000
SAVEHIST=50000
setopt EXTENDED_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_VERIFY
setopt SHARE_HISTORY
# Directory navigation options
setopt AUTO_CD
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_SILENT
# Fast Completion System (with caching)
autoload -Uz compinit
typeset -i updated_at=$(date +'%j' -r ~/.zcompdump 2>/dev/null || stat -f '%Sm' -t '%j' ~/.zcompdump 2>/dev/null)
if [ $(date +'%j') != $updated_at ]; then
compinit -i
else
compinit -C -i
fi
# Modern Keybindings: History search
bindkey -e
bindkey '^[[A' history-search-backward
bindkey '^[[B' history-search-forward
# Initialize Starship Prompt (if installed)
if command -v starship &> /dev/null; then
eval "$(starship init zsh)"
fi
무거운 프레임워크 없이 가벼운 플러그인 로딩
자동 제안과 구문 강조를 직접 로드:
# Clone plugins to ~/.zsh/plugins/
# git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/plugins/zsh-autosuggestions
# git clone https://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/plugins/zsh-syntax-highlighting
source ~/.zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Configure suggestion color
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
고급 Zsh 글로빙 및 매개변수 확장
재귀적 글로빙 및 수정자 활용:
# List all TypeScript files modified in the last 24 hours
ls -lh **/*.ts(m-1)
# Delete all empty directories recursively
rmdir -p **/*(/^F) 2>/dev/null
# Select only executable files in bin/
chmod +x bin/*(*)
일반적인 패턴
Zinit을 사용한 고속 플러그인 로딩
문제: Oh My Zsh 시작 시 무거운 즉시 로딩으로 인해 수백 밀리초가 소요됩니다. 해결책: 터보 모드(지연 로딩)와 함께 Zinit을 사용하세요.
# Install Zinit if missing
source ~/.local/share/zinit/zinit.git/zinit.zsh
# Turbo load syntax highlighting and autosuggestions asynchronously
zinit wait lucid for \
atinit"ZINIT[COMPINIT_OPTS]=-C; zicompinit; zicdreplay" \
zdharma-continuum/fast-syntax-highlighting \
atload"_zsh_autosuggest_start" \
zsh-users/zsh-autosuggestions \
blockf \
zsh-users/zsh-completions
강력한 셸 스크립팅 함수
문제: 옵션 파싱 및 오류 처리가 포함된 재사용 가능한 셸 함수 작성. 해결책: 표준 Zsh 매개변수 확장 사용.
# mkcd: Create directory and cd into it
mkcd() {
if [[ -z "$1" ]]; then
echo "Usage: mkcd <dir>" >&2
return 1
fi
mkdir -p "$1" && cd "$1"
}
모범 사례 (2026)
- 해야 할 일:
~/.zcompdump를 바이트 컴파일 파일(compdump.zwc)로 컴파일하여 셸 시작 시간을 가속화합니다. - 해야 할 일:
setopt SHARE_HISTORY를 설정하여 모든 활성 터미널 탭 간에 명령 기록을 동기화합니다. - 해야 할 일: 비대한 레거시 프롬프트 테마 대신 Starship이나 간결한 네이티브 프롬프트를 사용합니다.
- 해야 할 일:
~/.zshrc의 상단과 하단에zmodload zsh/zprof를 사용하여 시작 지연 시간을 프로파일링합니다. - 하지 말아야 할 일: 수십 개의 불필요한 플러그인이 포함된 무겁고 비대한 프레임워크 구성(모놀리식 Oh-My-Zsh 구성)을 사용하지 마십시오.
- 하지 말아야 할 일:
~/.zshrc에 평문 클라우드 자격 증명, API 토큰 또는 비밀을 저장하지 마십시오. 대신 개인.env파일이나 비밀 저장소를 사용하십시오. - 하지 말아야 할 일: 모든 대화형 셸 시작 시 느린 외부 명령(
nvm,brew doctor)을 동기식으로 실행하지 마십시오.
문제 해결
| 오류 / 증상 | 원인 | 해결책 |
|---|---|---|
.zshrc 편집 후 zsh: command not found: ... | .zshrc의 구문 오류 또는 $PATH 할당이 추가 대신 덮어쓰기됨 | 경로를 올바르게 추가: export PATH="$HOME/bin:$PATH"를 사용하고 source ~/.zshrc 실행. |
zsh compinit: insecure directories, run compaudit | /usr/local/share/zsh의 안전하지 않은 파일 권한 | `compaudit |
| Zsh 시작이 매우 느림 (> 500ms) | 동기식 nvm 또는 무거운 플러그인 초기화 | nvm에 지연 로딩 사용 또는 .zshrc 상단에 zmodload zsh/zprof, 하단에 zprof로 프로파일링. |