6

When I use set -x to debug a command it output some extra lines.

% set -x
+precmd_update_git_vars:1> [ -n '' ']'
+precmd_update_git_vars:1> [ '!' -n '' ']'
+precmd_update_git_vars:2> update_current_git_vars
+update_current_git_vars:1> unset __CURRENT_GIT_STATUS
+update_current_git_vars:3> [[ python == python ]]
+update_current_git_vars:4> _GIT_STATUS=+update_current_git_vars:4> python /home/ismail/zshfiles/gitstatus.py
+update_current_git_vars:4> _GIT_STATUS='' 
+update_current_git_vars:6> __CURRENT_GIT_STATUS=( '' ) 
+update_current_git_vars:7> GIT_BRANCH='' 
+update_current_git_vars:8> GIT_AHEAD='' 
+update_current_git_vars:9> GIT_BEHIND='' 
+update_current_git_vars:10> GIT_STAGED='' 
+update_current_git_vars:11> GIT_CONFLICTS='' 
+update_current_git_vars:12> GIT_CHANGED='' 
+update_current_git_vars:13> GIT_UNTRACKED='' 
+precmd_update_git_vars:3> unset __EXECUTED_GIT_COMMAND

I can't debug my commands because of these outputs.

Why is set -x debugging my .zshrc? I want set -x to debug only the lines followed by set -x.

2
  • 1
    Do you have your prompt configured to show information about git repositories? If so, it is debugging what was executed after set -x; that stuff is evaluated before your prompt is printed. Jul 5, 2020 at 0:25
  • Is there any condition, command or configuration which might be used so that the code in .zshrc do not interfere with any other commands, for example set -x. Jul 5, 2020 at 6:44

3 Answers 3

2

If you're trying to debug a script, then don't use set -x on the terminal (that's debugging the shell running in the terminal). You can instead start the script with the -x option to the interpreter (e.g., zsh -x <scriptname> [<args>]).

If, for example, you have a zsh script named ex.zsh, then you can do:

$ cat /tmp/ex.zsh
#!/bin/zsh

function () {
    echo "Hello, world!"
}
$ zsh -x /tmp/ex.zsh
+/tmp/ex.zsh:3> '(anon)'
+(anon):1> echo 'Hello, world!'
Hello, world!
1
  • 1
    Currently the solution that is working for me is bash -xv ./ex.sh Jul 6, 2020 at 4:15
1

OP here. i put the following lines on my .zshrc

alias debugOn='DEBUGCLI=YES exec zsh' 
alias debugOff='DEBUGCLI=NO exec zsh' 

if [[ $DEBUGCLI == "YES" ]]

then

export PS1="%F{013}%2~%f%(?.%F{004}.%F{001}✕%?)%# %f"

else 
. ~/zshfiles/zsh-git-prompt.zsh
export PS1="%F{013}%2~%f$(git_super_status)%(?.%F{004}.%F{001}✕%?)%# %f"

fi

Now It works like the following:

Documents/check:master✔% debugOn
Documents/check% set -xv              
Documents/check% echo "Debugging Line"
echo "Debugging Line"
+zsh:3> echo 'Debugging Line'
Debugging Line
Documents/check% set +xv              
set +xv
+zsh:4> set +xv
Documents/check% debugOff             
Documents/check:master✔% 
1

Running set -x in your shell puts "everything" into debug mode. But if you want to debug just a shell function, and anything it calls, you can use typeset -ft some_func to mark just that function for debugging. For example:

$ which some_func
some_func() {
   echo "hiii"
   ls -ls
   some_other_func 
}
$ typeset -ft some_func
$ some_func
....
<debug output here>
...

That'll scope the debug output to just the code you care about. Just run typeset -f +t some_func to turn debugging off. And if you want to debug a separate script independent of your enviorment run it with zsh -x script.sh like someone mentioned.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .