# /bin/sh built-in "trap", can't be piped



## Seeker (Jun 29, 2011)

Why this doesn't work?

```
trap 'echo LooooooL!!!' 2
trap | grep .

# This is ok
trap
```


----------



## Seeker (Jun 29, 2011)

This will work

```
...

fname=trap.txt

trap > "$fname"

echo "Listing $fname contest:"
cat "$fname"

rm "$fname"
```


----------



## jilles@ (Jun 29, 2011)

Anything can be piped but it will run in a subshell environment so that commands like trap will not print what you may expect. (POSIX allows but does not require this.) In our sh, you can do

```
traps=$(trap)
```
and it will return the parent shell environment's traps; this commonly works but is not ubiquitous.


----------



## Seeker (Jun 29, 2011)

That workaround works.


----------

