리눅스와 유닉스

.bashrc 파일이란?

Jake Seo 2024. 2. 4. 00:00

.bashrc 란?

  • Bash 가 시작될 때마다 실행되는 스크립트다.
  • Bash 는 대화형 셸 세션이다.
  • Bash 에 무언가 개인화된 기능을 넣을 때 필요하다.

.bashrc 의 일반적인 용도

  • 환경변수 설정 (environment variables)
  • 명령의 별칭 만들기 (alias)
  • 기본 편집기 설정
  • Bash 프롬프트 사용자 지정
  • PATH 변수에 디렉터리 추가

.bashrcBash 셸에서만 동작한다. Zsh 나 Fish 와 같은 다른 셸엔 자체 구성파일이 있다.

.bashrc 파일의 작성 예제

  • alias: 편의를 위한 별칭 설정
  • PATH: 사용자의 /bin 디렉터리가 있는 경우 이를 포함하도록 PATH 업데이트
  • 등등등...
# .bashrc

# Alias definitions.
alias ll='ls -la'  # List all files in long format
alias grep='grep --color=auto'  # Colorize grep output
alias df='df -h'  # Disk space in human-readable format
alias rm='rm -i'  # Interactive mode for rm

# PATH: Add user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# Environment Variables
export EDITOR='vim'  # Set default editor to vim
export GIT_EDITOR='vim'  # Set Git editor to vim

# Bash Prompt
export PS1='\[\e[0;33m\]\u@\h:\[\e[0;36m\]\w\[\e[m\]\$ '  # Colorized Bash prompt

# Load custom functions, if any
if [ -f "$HOME/.bash_functions" ]; then
    . "$HOME/.bash_functions"
fi

# Enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
fi

# Add git branch if its present to PS1
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[\e[0;33m\]\u@\h\[\e[0m\]:\[\e[0;36m\]\w\[\e[0;32m\]\$(parse_git_branch)\[\e[0m\]\$ "

# Check if window size has changed after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
반응형