zsh의 bash -x와 동일

zsh의 bash -x와 동일

방금 입력한 명령을 입력한 후 화면에 다시 표시되도록 하는 방법이 있습니까?

전임자:

$ echo hello
+ echo hello
hello

나는 이것이 가능하다는 것을 알고 있지만 bash -xzsh 매뉴얼에서 이에 상응하는 것을 찾을 수 없습니다.

답변1

-x(또는) 옵션도 -o xtrace적용됩니다 zsh. 이는 1970년대 후반의 Bourne 쉘에서 유래되었으며 모든 유사한 Bourne 쉘에서 지원됩니다. 에서 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.

대화형 셸에서 많은 멋진 플러그인이나 고급 완성 기능을 활성화하면 대화형 셸 환경에 영향을 줄 수 있는 실행에 해당하는 추적도 볼 수 있습니다.

답변2

부록 Will의 의견과 관련된 Andy Dalton의 정답...

시도해 보았지만 터미널에서 무작위로 여러 가지 내용이 출력되어 잘못된 내용이라고 생각했습니다.

zsh의 경우 Apple 터미널 애플리케이션에서 추적 혼란을 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

-dZsh 해결 방법은 precmdzsh-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 )

관련 정보