GNU Screen: Can't stuff commands unless the screen is attached?

Solution 1:

When you start a Screen session in detached mode (screen -d -m), no window is selected, so input later sent with screen -X stuff is just lost. You need to explicitly specify that you want to send the keystrokes to window 0 (-p 0). This is a good idea anyway, in case you happen to create other windows in that Screen session for whatever reason.

screen -S "$1" -p 0 -X stuff "$beast$(printf \\r)"

(printf \\r to strictly emulate the Return key; many but not all programs accept a newline (\n).)

Solution 2:

This recently came up while trying to answer a question on unix.stackexchange.com.

The summary is that screen does not have a default selected window unless you attatch, but Gilles showed us how you can force one to be selected by adding the argument -p 0 to your screen command.

Personally I recommend switching to tmux. Here is how you would port your screen commands to work in tmux:

tmux new-session -d -n $1
tmux send-keys -t $1 "$beast\n"