How to run a command inside screen such that you can get back to the command’s parent shell

gnu-screen

What's the best way to run a command inside a screen session such that its parent shell can be accessed? I want to be able to restart it from within the same screen session.

The best I've managed to come up with is the following:

$ cat mr-t.sh 
#!/bin/bash

top
exec /bin/bash

and then:

screen -e'^\\\' -S top-in-screen ./mr-t.sh

Then, if top stops running, I'll at least get a new shell within the same screen session I can work with. This isn't very elegant, though, and I can't quite convince myself that the signal will reliably be sent to the right process if I hit C-c. Moreover, C-z doesn't work at all.

I'm using bash 3.2.48 on OSX and 3.2.39 on Linux. Both are probably patched by OS vendors.

There's nothing special about the top command here, of course.

[As an aside, -e'^\\\' reassigns the Magick Screen Key from C-a (a bad default if there ever was one) to C-\.]

Best Answer

Create a ~/.screenrc.top like so:

screen
stuff "top\015"

Now run screen -c ~/.screenrc.top. No race condition!