Hi, This is a "RFC" to update documentation to better match behavior, or, to get some information about shell tracing.
Bellow I am quoting a request from an user: ---8<--- The bash shell offers the xtrace (set -x), functrace (set -T) and function trace (typeset -ft) facilities to control script and function tracing yet it appears that only set -x is effective, tracing the calling script + any and all called functions. set -x: Caller and all functions traced, irrespective of typeset [-+]ft and set [-+]T set +x: No trace at all, irrespective of trace -ft and set [-+]T In contrast, using the POSIX shell (HP-UX), the trace directives "set [-+]x" and "typeset [-+]ft " interact as follows (regardless of whether shell functions are defined with the "function name" or "name()" forms): set -x: - typeset -ft name ... Caller and named function(s) traced - typeset +ft name ... Caller but trace from named function(s) suppressed set +x: - typeset -ft name ... Only named function(s) traced - typeset +ft name ... No trace at all So function trace can be governed _independently_ of the calling script/session trace, which is useful in controlling the amount of trace generated. Can bash replicate this behaviour, i.e. trace a script but NOT its called functions ? ---8<--- Since access to HP-UX sh, that should be a superset of ksh88 is not easy, one shell that appears to be very close to the HP-UX sh in this respect is zsh, that allows either tracing or not tracing only functions. Looking at bash source code, it could require a significant change to offer such feature. First, I believe trace_p() would need to be converted to a three state, to know all the conditions, or, it could by default trace functions always and only not trace if trace_p() is unset. The problem is that trace_p() for functions is already being used for a different state control. Assuming the current trace_p() usage were modified, to implement disable of traced functions, a pseudo-patch could be: ---8<--- make_cmd.c:make_function_def() - temp->flags = 0; + temp->flags = att_trace; ---8<--- and ---8<--- execute_cmd.c:execute_function() restore_default_signal (ERROR_TRAP); } + if (echo_command_at_execute && !trace_p (var)) + { + unwind_protect_int (echo_command_at_execute); + echo_command_at_execute = 0; + } + + /* Shell functions inherit the RETURN trap if function tracing is on globally or on individually for this function. */ ---8<--- but to trace only functions under "set +x" a different approach would be required (the need for a three-state). Attached is a test script from the user, that will work with bash and zsh (to test ksh behavior need to comment the "set +-T"). Thanks, Paulo
x.sh
Description: Bourne shell script