배시에 관한 몇 가지 팁

참조 원문 : 8 More Bash Tips for Working Faster With the Shell

  날이면 날마다 오는 배시 관련 포스팅입니다.

앨리어스 등록
  일반적으로 앨리어스를 등록할 때는 ~./bashrc 파일에 아래와 같은 형식으로 원하는 내용을 집어넣습니다.

alias name=’command’

  그냥 이렇게 평범하게 등록하고 쓰는 방법도 있지만 앨리어스 전용 파일을 별도로 만들고 그 파일을 읽는다면 관리가 더 깔끔하겠죠? 이를 위해 .bash_aliases 파일을 만들어 앨리어스들을 넣은 후 ~./bashrc 파일에서 아래와 같이 파일을 읽어들입니다.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi


히스토리를 탐색할 때 페이지 업/다운 키 사용
  ~/.inputrc라는 파일을 만들고 아래의 두 줄을 집어넣으면 PageUp 키와 PageDown 키로 히스토리를 탐색할 수 있습니다. 파일을 만든 후 다시 로그인을 해야 적용됩니다.

“\e[5~”: history-search-backward
“\e[6~”: history-search-forward


이맥스 스타일의 단축키들
  화살표까지 손을 옮기기 귀찮을 때 사용하면 편합니다.

  • ^A – go to the start of line
  • ^E – go to the end of line
  • ^H – erase one character to the left
  • ^D – erase one character to the right, it also exits the shell by default if there is no character to delete
  • ^U – erase everything from the cursor to start
  • ^K – erase everything from the cursor to end
  • ^P – bring the previous command in history
  • ^N – bring the next command in history
  • ^C – interrupt character, sends SIGTERM to the current application


매뉴얼 페이지에 색깔 적용
  아래의 내용을 ~./bashrc 파일에 넣습니다.

export LESS_TERMCAP_mb=$’\E[01;31m’ # begin blinking
export LESS_TERMCAP_md=$’\E[01;38;5;74m’ # begin bold
export LESS_TERMCAP_me=$’\E[0m’ # end mode
export LESS_TERMCAP_se=$’\E[0m’ # end standout-mode
export LESS_TERMCAP_so=$’\E[38;5;246m’ # begin standout-mode – info box
export LESS_TERMCAP_ue=$’\E[0m’ # end underline
export LESS_TERMCAP_us=$’\E[04;38;5;146m’ # begin underline

  그리고 .bashrc 파일을 다시 읽어들입니다.

. ~/.bashrc

 


파일 이름의 뒷부분에 원하는 문자열 추가
  만약 ‘filename‘이란 이름의 파일이 있을 때 이 파일을 백업 파일로 만들 때는 ‘filename.backup‘ 같은 식으로 이름을 바꾸기 마련입니다. 이때 보통 아래와 같은 방법으로 이름을 바꿉니다.

mv filename filename.backup

  이때 아래의 형식으로 명령을 내리면 같은 효과를 볼 수 있습니다.

mv filename{,.backup}