I need to run a script by piping it through bash with wget(rather than running it directly with bash).

$ wget -O - http://example.com/my-script.sh | bash

It's not working because my script has read statements in it. For some reason these don't work when piping to bash:

# Piping to bash works in general
$ echo 'hi'
$ echo "echo 'hi'" | bash

# `read` works directly
$ read -p "input: " var
input: <prompt>

# But not when piping - returns immediately
$ echo 'read -p "input: " var' | bash

Instead of prompting input: and asking for a value as it should, the read command just gets passed over by bash.

Does anyone know how I can pipe a script with read to bash?


read reads from standard input. But the standard input of the bash process is already taken by the script. Depending on the shell, either read won't read anything because the shell has already read and parsed the whole script, or read will consume unpredictable lines in the script.

Simple solution:

bash -c "$(wget -O - http://example.com/my-script.sh)"

More complex solution, more for education purposes than to illustrate a good solution for this particular scenario:

echo '{ exec </dev/tty; wget -O - http://example.com/my-script.sh; }' | bash
| improve this answer | |
  • Awesome thanks. This is almost as elegant. – Robin Winslow Jan 22 '15 at 1:08

Process substitution will do what you want:

bash <(wget ...)

That said, I have to question your motivation here. If you control the webserver (and use https) then maybe this might make sense. But just running a script from the internet blind is very risky.

| improve this answer | |
  • just running a script from the internet blind is very risky - aye we know it's risky, but people can choose to trust scripts from certain organisations. See the discussion in comments on @Xen2050's answer. – Robin Winslow Jan 26 '15 at 13:33

Is there something wrong with :

wget -O tmpscript.sh http://example.com/my-script.sh
chmod +x tmpscript.sh


| improve this answer | |
  • 'cos then I have to create a file. I mean it'd work, it's just a bit messy. – Robin Winslow Jan 22 '15 at 1:08
  • 1
    True. Though that's a good reason to use /tmp, lots of programs use temp files. I'd be leary of running any downloaded scripts immediately without taking a look first... – Xen2050 Jan 22 '15 at 1:12
  • Well that's your call. Plenty of projects offer these one-liners, e.g. Docker, ohmyzsh and others I can't remember right now. In this case it's a setup script I want to run on new systems from my own repository. I'm not too worried about its contents, as I wrote it. – Robin Winslow Jan 22 '15 at 1:22
  • 3
    I'm thinking of anyone else finding this question (a totally new user perhaps) might not know any better. – Xen2050 Jan 22 '15 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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