views:

53

answers:

3

I want to be able to start a process and send input to it immediately.

Take Bash as an example.

The following code will enter another Bash process and then print "Hello World!" on the screen after I have terminated the process with "exit"

bash
echo "Hello World!"

Is there a way to enter bash and then print "Hello World!" INSIDE that process?

I'm using Ruby and Bash on Ubuntu.

UPDATE: This question was not intended to be Bash specific. Bash was just an example. It would be better if someone could post an answer that handles all other binaries.

+1  A: 
bash -c 'echo "Hello World!"'

You can also try writing bash script and invoking it:

bash ./myscript

or put #!/bin/bash as the first line in a text file and it will be invoked using bash like any other executable:

./myscript

Update0

Bash is an interpreter. There are many other interpreters, I'd highly recommend you take a look at Python, you can send instructions to be interpreted to these programs easily enough.

You might also be referring to the Unix IO-model, in which case you may want to ask a question relating to the use of piping with stdin and stdout.

Matt Joiner
A: 
%x(some external bash commands)

`ls -1`
I don't get what this has to do with my question.
never_had_a_name
+2  A: 

You may be looking for the expect tool.

caf
Looks very interesting.
never_had_a_name