bash alias 확장하기

KUBIC 실험 중 알아낸 정보입니다. KUBIC (KUBernetes Infrastructure for Containers)은 사내에서 진행 중인 kubernetes 기반 container 인프라 관리 프로젝트입니다.

kubernetes CLI client로 kubectl이 있는데 사용법은 다음과 같습니다. $ kubectl get po (pod 목록을 가져온다) $ kubectl create -f pod-filename.yaml (pod를 생성한다.)

매번 kubectl 이라고 치기 번거로워 .bash_aliases에 k=”kubectl” 정의하고

$ k get po
$ k create -f pod-filename.yaml

위와 같이 사용하다가 pod의 상태 변화를 보기 위해 다음과 같이

$ watch k get po

하면 ‘k not found’ 에러가 납니다.

man bash하여 alias 를 검색하니 alias는 첫 command에서만 검색이 되네요.

The first word of each simple command, if unquoted, is checked to see if it has an alias.

위에서 k는 watch 명령의 첫 인자값이므로 alias 확장이 되지 않습니다.

해결 방법은 watch도 alias에 넣는 겁니다.
man page를 보면,
If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

그렇다면, .bash_aliases에 watch=”watch ” 와 같이 뒤에 공백문자 하나 넣어 alias를 하나 더 만들어 아래와 같이 하면

$ . .bashrc
$ watch k get po

제대로 작동합니다.

.bashrc 안에 .bash_aliases를 읽는 부분이 있으므로 .bashrc를 다시 읽어들이면 새 alias가 됩니다.