Development Tip

.bashrc에 추가 파일 포함

yourdevel 2020. 11. 26. 19:58
반응형

.bashrc에 추가 파일 포함


시스템의 다른 파일에 존재하는 것을 선호하는 .bashrc에서 수행하고 싶은 작업이 있습니다. 이 파일을 .bashrc에 어떻게 포함시킬 수 있습니까?


다른 파일을 포함 할 위치에 추가 source /whatever/file(또는 . /whatever/file) .bashrc합니다.


오류를 방지하려면 먼저 파일이 있는지 확인해야합니다. 그런 다음 파일을 소싱하십시오. 이렇게하세요.

# include .bashrc if it exists
if [ -f $HOME/.bashrc_aliases ]; then
    . $HOME/.bashrc_aliases
fi

로드 할 파일이 여러 개 있고 존재하지 않을 수도있는 경우 for 루프를 사용하여 다소 우아하게 유지할 수 있습니다.

files=(somefile1 somefile2)
path="$HOME/path/to/dir/containing/files/"
for file in ${files[@]}
do 
    file_to_load=$path$file
    if [ -f "$file_to_load" ];
    then
        . $file_to_load
        echo "loaded $file_to_load"
    fi
done

출력은 다음과 같습니다.

$ . ~/.bashrc
loaded $HOME/path/to/dir/containing/files/somefile1
loaded $HOME/path/to/dir/containing/files/somefile2

먼저 버전을 확인하고 경로 구성에 변수를 할당하는 것을 선호합니다.

if [ -n "${BASH_VERSION}" ]; then
  filepath="${HOME}/ls_colors/monokai.sh"
  if [ -f "$filepath" ]; then
    source "$filepath"
  fi
fi

참고 URL : https://stackoverflow.com/questions/4952177/include-additional-files-in-bashrc

반응형