7

set -x just show a final result of the command. Any way to view each step more clear, like pics below.

enter image description here

3
  • Run bash in a debugger? Nov 28, 2016 at 18:03
  • I'm not sure if this does match your wishes: google.se/search?q=bashdb+debugger
    – Hannu
    Nov 28, 2016 at 18:52
  • how do you run the script ? did you try running it like this ? bash -x script_name.sh arg1 arg2 ?
    – Rabin
    Nov 28, 2016 at 19:06

2 Answers 2

2

It sounds like you'd like to have a shell syntax scanner and parser that outputs the found tokens and the actions it takes along the way to executing the actual command.

I'm not aware of any shell that does this as it would slow down processing immensely as well as producing an huge amount of output for each even slightly complex command.

With bash, if you compile the shell yourself with the DEBUG macro defined and if you enable all the commented out calls to the itrace() function in the shell's source code1, then you can get a bit of what you're wanting to see:

bash-4.4$ if [[ "$BASHPID" = 10 ]] && [[ "$USER" = myself ]]; then echo hello me; fi
TRACE: pid 22914: parse_matched_pair[9]: open = " close = " flags = 0
TRACE: pid 22914: parse_matched_pair[9]: returning $BASHPID"
TRACE: pid 22914: parse_matched_pair[9]: open = " close = " flags = 0
TRACE: pid 22914: parse_matched_pair[9]: returning $USER"
TRACE: pid 22914: param_expand: `$BASHPID' pflags = 0

Here, you can see the parser outputting a few debugging statements about finding opening and closing double quotes and shell variables. Later, another processing step informs us that a parameter expansion of $BASHPID is performed, but far from all steps are listed.

This type of debugging output from bash is more useful for people actually involved with writing and modifying the shell's source code though, and are definitely not intended to serve as a way of understanding the shells parsing and interpretation of its input.

To become more familiar with how the syntax and grammar of the shell works, I'd suggest starting at the POSIX definition of the shell's grammar (which is quite complex, and more or less meaningless to anyone who can't read EBNF).


1Except for one in lib/glob/glob.c that has a syntax error in it.

-2

set -x is for bash shell scripts, it won't work for binary files.

If you like to trace a binary execution, you should consider a debugger (e.g. gdb)

1
  • 1
    The question is asking specifically about bash, not arbitrary executables.
    – jayhendren
    Nov 28, 2016 at 18:38

You must log in to answer this question.

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