shスクリプトのデバッグ

shスクリプトに-xオプションを付けると実行するコマンドのトレースができる。便利だ。

$ man 1 sh
...
           -x xtrace        Write each command to standard error (preceded by
                            a `+ ') before it is executed.  Useful for debug-
                            ging.
...

実行例:

$ cat test.sh
#!/bin/sh

echo aaa
echo bbb
echo ccc
$ ./test.sh
aaa
bbb
ccc
$ sh -x test.sh
+ echo aaa
aaa
+ echo bbb
bbb
+ echo ccc
ccc