SundayJune
SundayJune

Reputation: 47

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?

Upvotes: 4

Views: 4618

Answers (1)

Alan Curry
Alan Curry

Reputation: 14711

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).

Upvotes: 12

Related Questions