키 바인딩을 사용하여 bash 구성을 다시 가져올 때 "명령을 찾을 수 없음" 오류를 방지하는 방법은 무엇입니까?

키 바인딩을 사용하여 bash 구성을 다시 가져올 때 "명령을 찾을 수 없음" 오류를 방지하는 방법은 무엇입니까?

내가 사용하는 bash버전 4.3.48:

$ bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

C-x r구성을 다시 로드하기 위해 키 시퀀스를 사용하여 키 바인딩을 정의하려고 합니다 bash + readline.@Gilles님 감사합니다, 내 코드에 다음 코드를 포함합니다 ~/.bashrc.

bind    '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": . ~/.bashrc'
bind    '"\C-xr":   "\e[99i~\e[99b~"'

bash클릭하면 C-x r이해할 수 없는 오류 메시지가 기록되는 것을 제외하고는 작동합니다 .

\udccf\udccf\udccf\udccf\udccf\udccf: command not found

나는 이것이 내가 변경한 다른 설정과의 상호 작용으로 인한 것일 수 있다고 생각하여 ~/.bashrc키 바인딩을 정의하는 3줄을 제외한 모든 항목을 제거했습니다 C-x r.

하지만 여전히 오류 메시지가 있습니다.

: command not found

그래서 저는 bashrc단 하나의 키 바인딩으로 더 간단한 접근 방식을 시도했습니다.

bind -x '"\C-xr": . ~/.bashrc'

이번에는 클릭하면 C-x r오류가 발생합니다.

\udccf
      : command not found

bind -x '"\C-xr": . ~/.bashrc'그러나 파일의 줄에 주석을 달고 bashrc대화형 셸에서 명령을 실행하면 bash모든 것이 잘 작동합니다. 오류 메시지가 없으며 구성이 올바르게 재구성되었습니다.

그래서 나는 bash(이미 완료된 경우) 재정의를 방지하기 위해 이 키 바인딩 주위에 가드를 작성해야 한다고 생각했습니다. 하지만 키 바인딩이 존재하는지 테스트하는 방법을 모르겠습니다. 또한 이러한 오류로 인해 어떤 일이 발생하는지 더 잘 이해하고 싶습니다.


이것이 중요한지는 모르겠지만 bash직접 시작하지는 않습니다. 항상 if 로 시작합니다 zsh. Bash 스크립트에 작성될 일부 명령을 테스트하려면 터미널(기본 셸로 사용됨)을 열고 zsh거기서 실행합니다. bash그렇다면 첫 번째 오류 메시지를 발생시킨 일부 환경 변수가 상속되었을 수 있습니까 bash?zsh

또한 댓글 섹션에서 요청한 대로 다음은 hexdump -C ~/.bashrc3개의 키 바인딩만 사용한 출력입니다.

00000000  62 69 6e 64 20 20 20 20  27 22 5c 65 5b 39 39 69  |bind    '"\e[99i|
00000010  7e 22 3a 20 72 65 2d 72  65 61 64 2d 69 6e 69 74  |~": re-read-init|
00000020  2d 66 69 6c 65 27 0a 62  69 6e 64 20 2d 78 20 27  |-file'.bind -x '|
00000030  22 5c 65 5b 39 39 62 7e  22 3a 20 2e 20 7e 2f 2e  |"\e[99b~": . ~/.|
00000040  62 61 73 68 72 63 27 0a  62 69 6e 64 20 20 20 20  |bashrc'.bind    |
00000050  27 22 5c 43 2d 78 72 22  3a 20 20 20 22 5c 65 5b  |'"\C-xr":   "\e[|
00000060  39 39 69 7e 5c 65 5b 39  39 62 7e 22 27 0a        |99i~\e[99b~"'.|
0000006e

bashrc다음은 다음만 포함하는 16진수 덤프입니다 bind -x '"\C-xr": . ~/.bashrc'.

00000000  62 69 6e 64 20 2d 78 20  27 22 5c 43 2d 78 72 22  |bind -x '"\C-xr"|
00000010  3a 20 2e 20 7e 2f 2e 62  61 73 68 72 63 27 0a     |: . ~/.bashrc'.|
0000001f

내 전체 콘텐츠는 다음과 같습니다 bashrc.

case "$-" in
  *i*) ;;
  *) return ;;
esac
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
  debian_chroot=$(cat /etc/debian_chroot)
fi
PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
alias cdr='cd "$(ls -dt */ | head -1)"'
export HISTCONTROL="ignoreboth:erasedups"
export HISTIGNORE="clear:history:"
export HISTFILE="${HOME}/.bash_eternal_history"
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
export PROMPT_COMMAND="history -a; history -c; history -r; ${PROMPT_COMMAND}"
cd() {
  if [ $# -eq 2 ]; then
    builtin cd "${PWD/$1/$2}"
  elif [ $# -eq 1 ]; then
    builtin cd "$1"
  else
    builtin cd
  fi
}
shopt -s autocd
shopt -s checkwinsize
shopt -s globstar
shopt -s histappend
shopt -s histreedit
shopt -s histverify
. /usr/share/bash-completion/bash_completion
. ~/.fzf.bash
. ~/.shrc
bind '" ": magic-space'
bind '"\C-xc": "\C-a\C-kvimdiff <() <()\C-b\C-b\C-b\C-b\C-b"'
bind '"\C-xf":  character-search'
bind '"\C-xF":  character-search-backward'
bind    '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": . ~/.bashrc'
bind    '"\C-xr":   "\e[99i~\e[99b~"'
bind '"\C-x\C-s": "sudo !!\C-m\C-m"'
bind -x '"\C-x\C-t": fzf-file-widget'
bind '"\C-t": transpose-chars'
bind '"\C-x\C-v": "\C-e | vim -R -\C-m"'
bind '"\em": "\C-aman \ef\C-k\C-m"'
bind '"\e\C-b":  "\C-e >/dev/null 2>&1 &\C-m"'

편집하다:

. ~/.bashrc함수 내부에 명령을 래핑 하고 키 바인딩에서 후자를 호출해 보았습니다. 다음 구문을 사용하십시오.

bind    '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": MyFunc'
bind    '"\C-xr":   "\e[99i~\e[99b~"'
MyFunc () {
  . ~/.bashrc
}

오류가 함수 이름의 길이에 영향을 받는 것으로 나타났습니다. 다음은 왼쪽 열에 다양한 함수 이름이 포함되어 있고 오른쪽 열에 오류 메시지(있는 경우)가 포함되어 있는 표입니다.

┌───────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────┐
│ a                 │ \udc99: command not found                                                                   │
│ ab                │ \udc92: command not found                                                                   │
│ abc               │ : command not found                                                                         │
│ abcd              │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found │
│ abcde             │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found       │
│ abcdef            │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found             │
│ abcdefg           │ no error                                                                                    │
│ abcdefgh          │ : command not found                                                                         │
│ abcdefghi         │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found                               │
│ abcdefghij        │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found                                     │
│ abcdefghijk       │ \udcdf\udcdf\udcdf\udcdf\udcdf: command not found                                           │
│ abcdefghijkl      │ \udcdf\udcdf\udcdf\udcdf: command not found                                                 │
│ abcdefghijklm     │ \udcdf\udcdf\udcdf: command not found                                                       │
│ abcdefghijklmn    │ \udcdf\udcdf: command not found                                                             │
│ abcdefghijklmno   │ \udcdf: command not found                                                                   │
│ abcdefghijklmnop  │ : command not found                                                                         │
│ abcdefghijklmnopq │ no error                                                                                    │
└───────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────┘

또한 명령 입력을 . ~/.bashrc터미널에 다시 연결하는 다른 기능을 시도했습니다(에서 영감을 얻었습니다).여기):

bind    '"\e[99i~": re-read-init-file'
bind -x '"\e[99b~": MyFunc'
bind    '"\C-xr":   "\e[99i~\e[99b~"'
MyFunc () {
  . ~/.bashrc </dev/tty
}

결과는 다음과 같습니다.

┌──────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ a                    │ 9}: command not found                                                                                                │
│ ab                   │ \udcdd: command not found                                                                                            │
│ abc                  │ : command not found                                                                                                  │
│ abcd                 │ no error                                                                                                             │
│ abcde                │ no error                                                                                                             │
│ abcdef               │ no error                                                                                                             │
│ abcdefg              │ no error                                                                                                             │
│ abcdefgh             │ : command not found                                                                                                  │
│ abcdefghi            │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found                                                        │
│ abcdefghij           │ \udcdf\udcdf\udcdf\udcdf\udcdf\udcdf: command not found                                                              │
│ abcdefghijk          │ \udcdf\udcdf\udcdf\udcdf\udcdf: command not found                                                                    │
│ abcdefghijkl         │ \udcdf\udcdf\udcdf\udcdf: command not found                                                                          │
│ abcdefghijklm        │ \udcdf\udcdf\udcdf: command not found                                                                                │
│ abcdefghijklmn       │ \udcdf\udcdf: command not found                                                                                      │
│ abcdefghijklmno      │ \udcdf: command not found                                                                                            │
│ abcdefghijklmnop     │ : command not found                                                                                                  │
│ abcdefghijklmnopq    │ no error                                                                                                             │
│ abcdefghijklmnopqr   │ no error                                                                                                             │
│ abcdefghijklmnopqrs  │ no error                                                                                                             │
│ abcdefghijklmnopqrst │ \udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf\udccf$: command not found │
└──────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

관련 정보