I tested a global set -x
with ksh88 (on Solaris 10) and ksh93 (Fedora 17) and with both a global set -x
command at the top of the script does not have function-local scope (i.e. it does not have any local effects).
As a workaround you can enable local command tracing for all functions in scope (after they are defined) and before they are called via typeset
:
$ cat test.ksh
PS4='$LINENO: '
set -x
function foo {
print Hello
}
bar() {
print World
}
typeset -ft `typeset +f`
foo
bar
Output under ksh88 (Solaris 10):
$ ksh test.ksh
13: typeset +f
13: typeset -ft bar foo
15: foo
1: print Hello
Hello
16: bar
1: print World
World
Typeset commented out
$ ksh test.ksh
15: foo
Hello
16: bar
World
Output under ksh93 (Fedora 17):
$ ksh test.ksh
13: typeset +f
13: typeset -ft 'bar()' foo
15: foo
6: print Hello
Hello
16: bar
10: print World
World
Typeset commented out
$ ksh test.ksh
15: foo
Hello
16: bar
10: print World
World
Output under bash
typeset
commented out and print
substituted with echo:
$ bash test.ksh
15: foo
6: echo Hello
Hello
16: bar
10: echo World
World
(bash 4.2.39(1) on Fedora 17)
Same output under zsh 5.0.2 on Fedora 17.
Conclusion
When using Ksh, only with ksh93 and the fnname()
function definition syntax a global set -x
also has local scope. The typeset -ft
based workaround is a relatively light way to enable command tracing for all functions.
In bash (and zsh) a global set -x
works as expected, i.e. it also has local scope for all functions.
Thus, when writing new scripts using bash instead of ksh might be the better alternative because of this.
As a side note: bash is probably even more portable than ksh88 - especially more portable than ksh93.
/usr/bin/ksh
on Solaris 10 also shows the described behaviour (no global effectset -x
). – Byssus