This question is similar to this one: https://serverfault.com/questions/342697/prevent-sudo-apt-get-etc-from-swallowing-pasted-input-to-stdin but the answer is not satisfying (appending && to each line of bash script is not elegant) and does not explain why some users can paste/execute multiple subsequent apt-get install -y commands and others can't because stdout is swollen by the next command.
I have a script my_script.sh:
sudo apt-get install -y graphicsmagick
sudo apt-get install -y libgraphicsmagick++1-dev
...
It can have only two lines or more of sudo apt-get install stuff. The libraries (graphicsmagick, etc.) doesn't matter, it can be any library.
When I copy this script and paste it's contents to bash or just execute it like this:
cat my_script.sh | sudo -i bash
then for some reason only the first line (graphicsmagick) gets executed and the rest is just printed to the console. It happens only with sudo apt-get install -y, other scripts, which doesn't contain this command behave normally.
If I change bash to sh (which is dash) I get expected behaviour:
cat my_script.sh | sudo -i sh
Can you explain why this happens?
When answering, can you please avoid this questions/comments:
- Why are you doing it this way?
- Piping to your bash is not safe
- Some other aspects are not safe or hackish
I just want to know why bash doesn't work as I would expect and sh does.
PS. I'm using Ubuntu 14.04, sh is dash as you can see here:
vagrant@vagrant-ubuntu-trusty-64:/tmp$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Feb 19 2014 /bin/sh -> dash
shisdash?ls -l /bin/shshould tell you. What OS are you using?sudoboth in your script and when invokingbashorsh.sudo apt-get -y install graphicsmagick<NEWLINE>sudo apt-get -y install libgraphicsmagick++1-devgetting executed differently bycat tmp.sh | bashandcat tmp.sh | dash. This is what a minimal example is about, and doesn't require people to either sudo-execute a downloaded script or reducing the problem themselves. Verified this on Mint 17; it seems likebashis somehow feeding the rest ofstdintoapt-getbefore the-yoption kicks in. Strange...