trap scope question

Hi all

I have a script that listens on a named pipe. That all works fine but I want to trap any kill signals and make sure I delete the named pipe on exit.

I can delete it fine exiting normally but cannot get the trap to see the variable holding the pipe name -

trap "rm -f ${PIPE};exit" EXIT

I have modified this to echo the value of PIPE into a file on exit -

trap "echo "P=$PIPE" > /tmp/mlog;rm -f ${PIPE};exit" EXIT

I see the P= but the value is blank.

How can I get the trap to see the variable?

Thanks

Single-quote the trap command so that the variable is evaluated only when the trap signal occurs and not when the trap is being set.

trap 'rm -f $PIPE;exit' EXIT
1 Like