Bash script is printed instead of executed
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 satisfactory (adding &&
to each line bash script is not nifty) and does not explain why some users can insert / execute multiple subsequent commands apt-get install -y
and others cannot because stdout is bloated 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 only have two lines or more sudo apt-get install
. Libraries (graphicsmagick, etc.) Doesn't matter, it can be any library.
When I copy this script and paste its contents into bash, or simply 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. This only happens with sudo apt-get install -y
, other scripts that do not contain this command behave normally.
If I change bash
to sh
(which is dash
) I get the expected behavior:
cat my_script.sh | sudo -i sh
Can you explain why this is happening?
By answering questions, you can avoid these questions / comments:
- Why are you doing this?
- The pipeline to your bash is not secure
- Some other aspects are not secure or hacky.
I just want to know why it bash
doesn't work the way I would expect and sh
does.
PS. I am using Ubuntu 14.04, sh is a 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
source to share