I'm running the following script on Ubuntu 14.04:


apt-get purge -y nginx
apt-get install -y nginx


When I run it like cat /tmp/script | bash, apt-get starts installing, then "date" is printed (not the actual date, but the command name), then the remaining apt-get output is printed.

If however I run the script like /tmp/script, it works as expected: printing the date after apt-get finished.

Why does this happen and how can I force bash to work when being piped to the same way it does when invoked directly?

  • I cannot reproduce that (with other commands). What happens if you put echo foo # at the beginning of the apt-get lines? Have a look at the output of od -t c -t x1 /tmp/script. Are there only newlines or maybe \rs, too? – Hauke Laging Feb 3 '15 at 9:45
  • @HaukeLaging, with echoes it works as expected. There are no \r symbols. BTW my bash version is 4.3.11 and this is the default ubuntu 14 image provided by AWS – Fluffy Feb 3 '15 at 10:05

You'll have to close (or otherwise redirect) the standard input of the individual commands:


apt-get purge -y nginx <&-
apt-get install -y nginx <&-


Otherwise subsequent line are fed to the commands.

| improve this answer | |
  • I now get infinite E: Write error - write (14: Bad address) messages – Fluffy Feb 3 '15 at 10:47
  • @Fluffy try redirecting in from /dev/null, instead. Oh, wait. "Write error"? – muru Feb 3 '15 at 10:49
  • Yes, either way, < /dev/null helped, thanks – Fluffy Feb 3 '15 at 11:26

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.