0

that's my code

bash << EOF
  read
  EOF

here, the read command fails and it doesn't read from the std input

and here

bash << EOF
  read
  ls
  ls
  EOF

only the second ls is executed (the first ls was redirected to read as an input?)

can anyone tell me what I'm doing wrong here? all commands that expect an input from the std input fails

4
  • bash's stdin is tied up getting the commands to run. Feb 21, 2014 at 21:28
  • What are you expecting to happen? Put another way, what are you trying to do? When you use a here-doc, the content is not expected to be changing underneath it . . . read is not returning since there's nothing to read?
    – ernie
    Feb 21, 2014 at 21:43
  • well, I needed to write the code this way to make an arch install script (part of it here), but, the passwd command fails here (the password is expected to be putted by user later when running the script)
    – lejenome
    Feb 21, 2014 at 21:58
  • It can be fixed by reading the password outside the block. but, I want to know why it doesn't work on this case
    – lejenome
    Feb 21, 2014 at 22:01

1 Answer 1

1

The <<word syntax ("Here documents") redirects standard input. So what you've done is start up a bash whose standard input is redirected to the "here document". When that bash starts up children (or executes builtin commands), these inherit standard input from the bash, so they are also reading from the here document. (There is nothing particularly mysterious about here documents. One implementation possibility is to copy the here document into a temporary file, and then redirect standard input to that file. Bash may do that under certain circumstances.)

Redirecting standard input is not the only way to get bash to execute a file. You can just give bash the name of the file on the command line. Again, you don't need a real file for this to work; you can use process substitution, like this:

bash <(echo '
 read -p "Say it: " A
 echo You said $A
')

Since that doesn't redirect standard input, the read will read from standard input, not from the commands fed into bash.

Watch out for quotation problems, though: they bit me twice typing that simple example.

1
  • thnx, that exactly what i needed :) I need to learn more about i/o redirection on bash
    – lejenome
    Feb 22, 2014 at 0:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .