Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm following this guide on how to set up passwordless SSH authentication with ssh-agent.

To start up ssh-agent the author recommends the following code in .bash_profile:

SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
    eval `$SSHAGENT $SSHAGENTARGS`
    trap "kill $SSH_AGENT_PID" 0
fi

I don't understand why it is trapping signal 0. According to man 7 signal there is no such signal.

Is this just a typo or bug or does this really achieve something?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

From the bash manual:

trap [-lp] [[arg] sigspec ...]

... If a sigspec is EXIT (0) the command arg is executed on exit from the shell.

share|improve this answer
9  
Correct. In case a reader came here because of the question's title, note that this is the meaning of 0 in a shell trap command. When sending a signal with kill, a signal number of 0 means not to send a signal after all; this is a way of testing the existence of the target process. –  Gilles Jul 24 '11 at 14:00
    
@Mat: Thanks a lot. I was reading man 1 trap... –  Legate Jul 24 '11 at 14:30
    
But it's not in the list output by trap -l! –  amphetamachine Jul 25 '11 at 8:59

trap on 0 is run when the shell exits. It is commonly used to clean up tmp files in one place that is always executed:

tmp=/tmp/myscript.$$

trap 'rm -f $tmp; exit' 0 1 2 15

do_a_bunch_of_stuff

exit

the exit at the end of the trap exits the shell at the cleanup with the right status.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.