次のスニペットを実行します。
#!/bin/bash
function preexec ()
{
echo -e "\n-->preexec command: $BASH_COMMAND"
}
trap 'preexec' DEBUG
function testfunc ()
{
echo "testfunc called $1"
}
testfunc "main"
source "source.sh"
exit 0
ここで、source.shは
#!/bin/bash
testfunc "source"
与える:
-->preexec command: testfunc "main"
testfunc called main
-->preexec command: source "source.sh"
testfunc called source
-->preexec command: exit 0
これは、ソースファイル内のすべてのコマンドがDEBUGトラップによってトラップされないことを意味します。
実際、行を追加すると
trap 'preexec' DEBUG
2行目のsource.sh内では、すべてが希望どおりに機能します(ソースファイル内のコマンドもトラップされます)。
ソースを作成する必要のあるファイルに対して上記の行が繰り返されないようにするには、これをデフォルトの動作にするにはどうすればよいですか?言い換えると、ソースファイルにDEBUGトラップを継承するように指示する機会はありますか?