
有沒有辦法讓我剛剛輸入的指令在輸入後回顯到螢幕上?
前任:
$ echo hello
+ echo hello
hello
我知道可以這樣做,bash -x
但我在 zsh 手冊中找不到等效的內容。
答案1
-x
(或)選項也-o xtrace
適用zsh
。它來自 70 年代末的 Bourne shell,並受到所有類似 Bourne shell 的支持。從man zshoptions
/ info zsh xtrace
:
XTRACE (-x, ksh: -x) Print commands and their arguments as they are executed. The output is preceded by the value of $PS4, formatted as described in the section EXPANSION OF PROMPT SEQUENCES in zshmisc(1).
例子:
#!/bin/zsh -x
echo hello
和一個運行範例:
$ /tmp/ex.sh
+/tmp/ex.sh:3> echo hello
hello
與bash
/中一樣ksh
,可以使用set -x
或啟用它set -o xtrace
,然後使用set +x
或停用它set +o xtrace
。還可以使用functions -t myfunction
.
請注意,在互動式 shell 中,如果您啟用了許多奇特的插件或高級完成功能,那麼您還將看到與可能影響您的互動式 shell 體驗的那些執行相對應的追蹤。
答案2
附錄安迪道爾頓的正確答案,相對於威爾的評論...
我嘗試過,但它導致我的終端輸出一堆隨機的東西,所以我認為這是不正確的。
對於 zsh,add-zsh-hook -d precmd update_terminal_cwd
可用於減少XTRACE
Apple 終端應用程式中的痕跡混亂。
長話短說
對於蘋果的終端應用程序,update_terminal_cwd()
每次提示更新時都會執行一個附加程式。
該update_terminal_cwd
呼叫也顯示在 'set -x' 中,並增加了混亂XTRACE
。
username@hostname ~ % echo hello
# +-zsh:2> echo hello
# hello
# +update_terminal_cwd:5> local url_path=''
# +update_terminal_cwd:10> local i ch hexch LC_CTYPE=C LC_COLLATE=C LC_ALL='' LANG=''
# +update_terminal_cwd:11> i = 1
#
# … <snip>
#
# +update_terminal_cwd:22> printf '\e]7;%s\a' #file://hostname.local/Users/username
/etc/bashrc_Apple_Terminal
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL, including
# the host name to disambiguate local vs. remote paths.
# … <snip>
printf '\e]7;%s\a' "file://$HOSTNAME$url_path"
}
PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
Bash 解決方法:unset PROMPT_COMMAND
或修改PROMPT_COMMAND
為不使用update_terminal_cwd
.
/etc/zhrc_Apple_Terminal
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL, including
# the host name to disambiguate local vs. remote paths.
# Percent-encode the pathname.
local url_path=''
{
# … <snip>
}
printf '\e]7;%s\a' "file://$HOST$url_path"
}
# Register the function so it is called at each prompt.
autoload -Uz add-zsh-hook
add-zsh-hook precmd update_terminal_cwd
-d
Zsh 解決方法可以從precmd
zsh-hook中刪除來完成:
### `-L` list
user@host ~ % add-zsh-hook -L
# typeset -g -a zshexit_functions=( shell_session_update )
# typeset -g -a precmd_functions=( update_terminal_cwd )
user@host ~ % add-zsh-hook -d precmd update_terminal_cwd
user@host ~ % add-zsh-hook -L
# typeset -g -a zshexit_functions=( shell_session_update )
user@host ~ % set -x
user@host ~ % echo hello
# +-zsh:8> echo hello
# hello
user@host ~ % set +x; add-zsh-hook -L
# typeset -g -a zshexit_functions=( shell_session_update )