As an aside: have you thought about using agent-shell?

https://github.com/xenodium/agent-shell