Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

Possible Duplicate:
sending text input to a detached screen

With reference to the answered question: Sending input to a screen session from outside

I am trying to write a script that will create a screen and then stuff input (in my case, UNIX commands) into the shell that I want it to execute.

Here's what I have till now:

$ screen -dmS new_screen bash

$ screen -S new_screen -X stuff "cd /some/path/
" <--This new line is required to execute the above command. 
$ # Note: See comments. We could insert the newline character instead of 'hard-coding' a newline string literal.

For some reason, the screen gets created fine and enters the bash shell. But the cd and some_script_file.sh commands don't get stuffed to the screen's bash shell.

Now the really strange part is.. After I detach from the screen, re-execute the stuff command as below..

$ screen -S new_screen -X stuff "cd /some/path/

and then attach back into new_screen. Now I see that the commands were correctly stuffed and executed in the bash shell inside screen.

Not sure where I am going wrong here, there seems to be any issue stuff-ing commands into a not as yet attached screen.

share|improve this question

marked as duplicate by Gilles, jasonwryan, Renan, Mat, Caleb Nov 25 '12 at 23:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

In tmux you have to provide the ENTER word to signify you are done. Pretty sure you need to do the same with screen. – vgoff Nov 18 '12 at 6:05
@vgoff I guess that is handled when we add the newline character placing the double quotes on the new line. I guess an alternative way would be to write $ screen -S new_screen -X stuff "cd /some/path/^M some_script_file.sh^M" – Kent Pawar Nov 18 '12 at 6:25
Let me try this and get back to you... – Kent Pawar Nov 18 '12 at 6:25
@vgoff Yup it worked. I replaced the newline literals with the UNIX newline character (represented as ^M). This is done by opening the script in vi, then in insert mode I presses [CTRL] + [V] followed by [ENTER] to insert ^M (return key). – Kent Pawar Nov 18 '12 at 7:33
up vote 3 down vote accepted

When a screen session is first created, no window is selected. As such, when you send your commands, screen doesn't know which window to send it to. Once you have attached to the screen, however, the window has become selected, which is why you can send commands after doing so. To select a window from the command line you need to use the -p option.

Here's a modification of your command that should work:

screen -S new_screen -p 0 -X stuff "cd /some/path/
share|improve this answer
Thanks @Chris! Yes that seemed to be the issue. I just came across a similar issue discussed here sending-text-input-to-a-detached-screen; but you have summarized it well. – Kent Pawar Nov 18 '12 at 3:47
So a return literal inside the string. Would this work with pipes? Would you still need the return as the last character? – vgoff Nov 18 '12 at 6:28
@vgoff I added a post below to answer your questions.. – Kent Pawar Nov 18 '12 at 7:46
I literally watched you post them... the comment and the edit. – vgoff Nov 18 '12 at 7:48
Haha Okay. Well I am new to scripting and just wanted this post to be as informative as possible.. – Kent Pawar Nov 18 '12 at 8:08

@vgoff: "So a return literal inside the string. Would this work with pipes? Would you still need the return as the last character? – vgoff"

Looking at using a return literal or a return character (^M):

bash-3.00$ echo "(1) This sentence
> (2) has
> (3) 3 returns
> (4) in it.
> " | grep "has";
(2) has

Well looks like a return literal does not affect pipping. And we need a return before the closing quote to act as a RETURN key to send the echo statement for execution.

share|improve this answer
My comment was in regards to stuffing to screen. You don't do that here, and so the experiment, though a fine experiment, doesn't answer the question in regards to 'stuffing screen'. You can edit this answer though, so that is good. – vgoff Nov 18 '12 at 12:05

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