I'm currently writing a Bash script that creates an SSH tunnel. Once this is done, I would like the script to open a new local (!) interactive shell so that I can work on my system with the SSH tunnel open.

Once I type exit the local shell should be closed, and the original script continues to run.

My current approach basically looks like this:

#!/usr/bin/env bash

set -e

ssh -N user@ip -L xxx:xxx &

trap "kill ${SSH_CONNECTION}" ERR

sleep 3
bash -i


So far, everything works - except running the local shell. I actually get a shell, and I can even type in it, but nothing happens when I hit <Return>.

Any idea what I'm doing wrong?

  • Where did you found such strange trap's signal ERR ? AFAIK it doesn't exist. (Proof: /bin/kill -L)
    – Alex
    Feb 1 '17 at 10:08
  • Ohh, it is bashism, sorry, missed that, ignore previous then
    – Alex
    Feb 2 '17 at 9:00

You need to use -f so the connection will go to background once it will be established. Then you will not need the &:

ssh -Nf  -L xxx:xxx user@ip

and the processing should get into your bash.

  • As I mentioned, I wanted to run a local shell, not a remote one. So, unfortunately, this does not work :-/
    – Golo Roden
    Feb 2 '17 at 19:45
  • did you read and try that?
    – Jakuje
    Feb 2 '17 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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