警告:行編輯未啟用

警告:行編輯未啟用

我發現這個有用的命令bind -x '"\C-r"':reset可以清除終端,但我想製作一個簡單的 bash 腳本:

#!/bin/bash
bind -x '"\C-r"':reset

輸出:

alfred@alfred-laptop:~/bash$ ./bind 
./bind: line 2: bind: warning: line editing not enabled

有人可以解釋一下嗎:

  1. 我怎樣才能解決這個問題?
  2. 這是什麼warning: line editing not enabled意思?

答案1

您需要取得該腳本的來源。執行. ./bindsource ./bind以使該鍵綁定在目前會話中處於活動狀態。

正常運行它,它沒有終端,所以它會給你錯誤訊息。此外,如果它要工作,它只會在腳本持續時間內處於活動狀態。

如果您希望該鍵綁定持久存在,請將該命令新增至您的~/.bashrc.

答案2

我收到了類似的訊息,但我的消息來自在互動式(登入)shell 之外運行的腳本;它是一個透過 CGI 腳本運行的 shell 腳本。我的信息是:

/home/richard/.bash_profile: line 4: bind: warning: line editing not enabled

儘管它實際上不在第 4 行,但bind該文件中唯一的內容是:

bind 'set completion-ignore-case on'

當然,這只有在啟用行編輯的情況下才有意義,即如果它是互動式 shell。

答案3

只是想改進已接受的答案。

line editing not enabled在非互動式 shell 中運行時,您會得到“ ”,因為bind需要 TTY。

將您的命令包裝bind在檢查中,如下所示:

if [ -t 1 ]
then
    # standard output is a TTY
    bind -x '"\C-r"':reset
fi

答案4

我會測試 shell 是否是互動的。
這是我在發行版的 bashrc 檔案中找到的一段程式碼

# Test for an interactive shell.  There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
# if [[ $- != *i* ]] ; then
#   # Shell is non-interactive.  Be done now!
#   return
# fi

相關內容