zsh의 bash -x와 동일

zsh의 bash -x와 동일

방금 입력한 명령이 입력 후 화면에 에코되도록 하는 방법이 있나요?

전:

$ echo hello
+ echo hello
hello

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

답변1

-x(또는 -o xtrace) 옵션도 작동 합니다 zsh. 이는 70년대 후반 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 -xset -o xtraceset +xset +o xtracefunctions -t myfunction

대화형 셸에서 여러 멋진 플러그인이나 고급 완성 기능을 활성화한 경우 대화형 셸 환경에 영향을 미칠 수 있는 플러그인의 실행에 해당하는 추적도 볼 수 있다는 점에 유의하세요.

답변2

Will의 의견과 관련하여 Andy Dalton의 정답을 추가합니다.

시도해 보았지만 터미널에서 무작위로 여러 가지 내용이 출력되었기 때문에 그것이 옳지 않다고 생각했습니다.

zsh의 경우 Apple 터미널 앱에서 add-zsh-hook -d precmd update_terminal_cwd추적 혼란을 줄이는 데 사용할 수 있습니다 .XTRACE

TL;DR

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

Zsh 해결 방법은 zsh-hook -d에서 삭제 하여 수행할 수 있습니다.precmd

### `-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 )

관련 정보