简体   繁体   中英

Zsh inherit xtrace option

Similar to this question: bash recursive xtrace , but for Zsh.

How can I make all the Zsh subshells inherit the xtrace option?

For example, if script1.sh is calling ./script2.sh and I run zsh -x script1.sh , I want script2.sh to also have the xtrace mode enabled.

For Bash, the answer is to export the SHELLOPTS variable.

Is there a solution for Zsh?

You can take advantage of startup/shutdown files in Zsh (see man zshall ): Zsh will always read (and execute) the file $ZDOTDIR/.zshenv at startup (if ZDOTDIR is unset, HOME is used instead). So you can put set -x in $ZDOTDIR/.zshenv and every Zsh script will run with xtrace mode enabled.

This is how I used it in a script:

env_dir="$(mktemp -d)"
echo "set -x" > "${env_dir}/.zshenv"
export ZDOTDIR="${env_dir}"
zsh "$@"
unset ZDOTDIR
rm -rf "${env_dir}"

In fact, this solution can be used for Bash as well, using the BASH_ENV variable, which points to a file that will, similarly, be executed when Bash starts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM