Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why my trap doesn't work when the signal set as "DEBUG" fake signal?

#test code:

#!/bin/bash
#~/test/test.sh
trap "echo 'testmessage'" DEBUG

while :
do
echo abc
sleep 6
done

#run it
~/test$sh test.sh

==============================

#result
=>   trap: DEBUG: bad trap  

==============================

?[shell debug] why my trap doesn't work when the signal set as "DEBUG" fake signal,but report trap error?

like image 693
SundayJune Avatar asked Dec 21 '22 19:12

SundayJune


1 Answers

The error message "bad trap" is produced by ash, not bash. When you run sh test.sh the shebang line is irrelevant because you aren't executing the script, you're executing the program called "sh" with "test.sh" as an argument. The sh program (in your case a symlink to ash or dash, I bet) then does its best to run the script named in the argument. The shebang line would come into play if you ran the command ./test.sh (it'll need +x permission first).

like image 76
Alan Curry Avatar answered Dec 23 '22 08:12

Alan Curry