Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

I'm writing a CLI script that asks a series of questions before doing a few things. How can I pass them as arguments so that I don't have to keep entering them every time I want to test my script?

Basically, it should pass 4 items to STDIN, like "text1[ENTER]text2[ENTER]text3[ENTER]text4[ENTER]" automatically.

Yes, I could modify my script to actually read the shell arguments, but I don't want to do it that way, since it's supposed to run more like a wizard.

Looking for something like

SOMEPROGRAM myscript arg1 arg2 arg3 arg4


SOMEPROGRAM arg1 arg2 arg3 arg4 | myscript

Or something like that. Is there such a program?

share|improve this question

I understand you do not want to modify myscript.

Regarding the second solution you ask for, you can use printf:

printf '%s\n' text1 text2 text3 text4 | myscript

so that, defining an alias for SOMEPROGRAM as:

alias SOMEPROGRAM="printf '%s\n'" 

you could effectively call

SOMEPROGRAM text1 text2 text3 text4 | myscript

The first form is ambiguous (from the point of view of SOMEPROGRAM), because it don't know where myscript options end and text parameters start, unless myscript is effectively invoked without any options. In this case you could use a function:

  printf '%s\n' "$@" | "$myscript"

so that you could effectively call

SOMEPROGRAM myscript text1 text2 text3 text4
share|improve this answer didn't have to make it verbatim :p printf solution looks good, I'll have to try this on monday! – mpen Sep 10 '11 at 0:52
for the printf solution I get stty: stdin isn't a terminal – mpen Sep 13 '11 at 20:17
@Mark: ok, not all programs accept indifferently stdin or interactive input. What is your myscript? – enzotib Sep 13 '11 at 21:19
It's a PHP script that takes input in via $fp=fopen("php://stdin","r") and fgets($fp) – mpen Sep 13 '11 at 21:56
@Mark: I have no php knowledge, but I wonder why the stty string in the error message. It could be useful to see a stripped down, but complete, version of myscript that still presents the problem. – enzotib Sep 14 '11 at 4:16

You can pass the parameters, but it won't work, you'll also have to modify the bash script to accept the params from the command-line as well.

share|improve this answer
Why's that? It's just reading in 4 things from STDIN one at a time, and you can pass things into I don't see the problem. – mpen Sep 10 '11 at 0:52

So basically, you want to pass each argument as a line to a child program. Below is a script that loops through all arguments passed ti SOMEPROGRAM and prints them as a line. You can pass an empty line ("enter without entering something before") by passing an empty argument as in SOMEPROGRAM yes '' | myscript.

# This is "SOMEPROGRAM", it prints each argument as "answer" to  a program that
# must be piped. Usage: SOMEPROGRAM yes 1 no 2 | myscript
for arg; do
    echo "$arg"

If you always need to answer "yes" to your script, use the coreutil yes:

yes | myscript

If you need to pass some other value, say "no":

yes 'no' | myscript
share|improve this answer

If you are writing a script then just use getopts so the arguments are passed to the scripts and then you can use those args as you want? Is this something you are looking for?

share|improve this answer
I'm just going to quote myself here... "Yes, I could modify my script to actually read the shell arguments, but I don't want to do it that way, since it's supposed to run more like a wizard." – mpen Sep 10 '11 at 0:53

For simple cases, the way enzotib describes it with piping stdin from a file seems reasonable. Consider this script:

read -p "What's your name?" name 
echo $name 
read -p "Age?" age 
echo $age
echo "Your name is " $name " and your age is "$age

invoke it with:

echo -e "Foobar\n14\n" | ./ 

The -e is to enable escape sequences like \newline.

Your name is  Foobar  and your age is 14

Note how magically, bash doesn't print the prompt for reading.

However, for more complicated cases, there is a program, called expect to handle interactive CLI programs, where you have to wait for the next prompt, before entering the second value, and I guess you can even branch under conditions.

share|improve this answer
echo solution looks nice too. It's actually a PHP CLI script, but I follows roughly the same format as your example, so I think it should work. – mpen Sep 10 '11 at 0:57

With bash, you can do this:

myscript << END


myscript <<< $'arg1\narg2\narg3\narg4\
share|improve this answer

I'm pretty sure you're looking for expect.

edit: example added

expect -c "  
spawn ssh $USER@$HOST  
expect \"password:\"  
send \"$PASS\r\"  

--from this blog

share|improve this answer
But then you have to write a whole expect script? I was hoping I could just stuff in some command-line args, or pipe it through some program without writing a whole secondary script. Maybe I was just dreaming though. – mpen Feb 21 '12 at 17:54
check the's not a whole different script exactly as it's still inside the same bash script – hbdgaf Feb 21 '12 at 20:59
Except I was writing a PHP CLI script, not a CLI script. Although I could probably shell_exec it...not sure if it would work like that or not though. Oh well. – mpen Feb 21 '12 at 22:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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