Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Bash by danamerr ( 3 years ago )
#This doesn't work

set -o errexit
trap 'catch $? $LINENO' EXIT
catch() {
  code="$1"
  echo "Exit code $code"
}
purposely break here

#The above will fail miserably on code="$1". The echo is never run, and the script exits on code=

#This works

set -o errexit
trap 'catch $? $LINENO' EXIT
catch() {
  local code="$1"
  echo "Exit code $code"
}
purposely break here
#Declaring code as a local variable works.
#If I put echo whatever before code= also works.
#I don't know why this fails, but I tried many variations of "why is this broken?", and my final answer is "bash trap function HATES global variable assignment as first line in the invoked trap function".

 

Revise this Paste

Your Name: Code Language: