Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Example: In a shell script I open a SSH tunnel whit this:

/usr/bin/expect <<EOD
set timeout -1
spawn ssh -fNL localhost:1873:localhost:873 HOST
expect "*?assword:" { send "$SSHPASS\r"}
sleep 2
exit [exp_pid]

export SSH_PID=$?
echo $SSH_PID

now $SSH_PID has some number, but it is not the PID of the spawned ssh. I tried different ways I found but non worked. Any Idea?

share|improve this question

Exit status are 8 bit intergers (though most shells (ksh93 being an exception) will truncate it to 7 bits as they use bit 8 for signaled processes), so it will only work if the pid is between 0 and 127. Get the pid from a pipe instead of the exit status:

expect <<EOD
spawn -noecho ssh ...
puts [exp_pid]
share|improve this answer

Your Answer


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.