使用 $_ 時出現“__bp_preexec_invoke_exec:沒有這樣的檔案或目錄”

使用 $_ 時出現“__bp_preexec_invoke_exec:沒有這樣的檔案或目錄”

我正在嘗試建立一個新目錄,並同時使用 Bash 在一行命令中切換到該目錄mkdir repo && cd $_。以前,這對我來說在所有 Linux 發行版上都運行良好,但現在當我在 Elementary OS 5 上嘗試它時,它會拋出以下錯誤:

:~$ mkdir repo && cd $_
bash: cd: __bp_preexec_invoke_exec: No such file or directory

這是 Bash 問題嗎?我們該如何解決這個問題?

答案1

這不是sole Bash的問題。

我的猜測是(終端模擬器?)與 Bash 集成,定義__bp_preexec_invoke_exec函數並設置DEBUG使用該函數的陷阱。

SO有個問題:bash:保存$_DEBUG陷阱中。從那裡:

使用DEBUG陷阱時,$_基於陷阱 [...] 運行的最後一個命令,而不是用戶輸入的最後一個命令

回答:

值得注意的是,“最後執行的命令的最後一個參數”包括文字文字“最後執行的命令”,而不是“使用者輸入的最後一個命令”; bash 在這方面的表現正如其文件所承諾的那樣。

但不用介意:除非你的陷阱返回非零值(從而中止它們之前運行的命令),否則這很容易解決:

trapfunc() { local old_=$1; date; : "$old_"; }
trap 'trapfunc "$_"' DEBUG

以 iTerm2 為例。它使用__bp_preexec_invoke_exec(請注意,在您的特定情況下,它可能是其他一些出於相同目的使用相同名稱的程式)。目前我正在寫這個答案,這就是您可以在下面找到的內容https://iterm2.com/shell_integration/bash:

# This function is installed as the DEBUG trap.  It is invoked before each
# interactive prompt display.  Its purpose is to inspect the current
# environment to attempt to detect if the current command is being invoked
# interactively, and invoke 'preexec' if so.
__bp_preexec_invoke_exec() {
    # Save the contents of $_ so that it can be restored later on.
    # https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702
    __bp_last_argument_prev_command="$1"

該函數繼續執行,然後

__bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"

}

以及其他地方 ( __bp_install):

trap '__bp_preexec_invoke_exec "$_"' DEBUG

所以它基本上使用連結答案中的解決方案。請注意,程式碼甚至提到了連結的問題!

您應該找出您的來源__bp_preexec_invoke_exec,並根據可能的trap '__bp_preexec_invoke_exec' DEBUG線路對其進行相應的修補。或者可能是罪魁禍首的軟體已經被修補,你只需要更新。

相關內容