Macos – How to paste multiple Bash commands into a shell without losing some

bashmacosshellssh

It's nice to copy-paste a series of Bash commands that you find on a website. But depending on the commands, sometimes you lose a few. Maybe they get swallowed up by programs that read from standard input, or perhaps there's another explanation.

So I end up doing this sometimes:

$ bash <<EOF
cmd2
...
EOF

Is there a better way? Some Bash option? An SSH option? (My setup is an Bash running on an Ubuntu server, which I'm SSH'ed to from a standard OS X terminal. Not sure how much of that is relevant.)

EDIT

Example

In response to requests for a concrete example, here's one. I pasted the following four lines into an SSH shell (from my Snow Leopard desktop) connected to a stock Ubuntu Quantal running on an OpenStack VM, in the Bash shell.

sudo apt-get install -y r-base gdebi-core
sudo apt-get install -y libapparmor1 # Required only for Ubuntu, not Debian
wget http://download2.rstudio.org/rstudio-server-0.97.314-amd64.deb
sudo gdebi rstudio-server-0.97.314-amd64.deb

The first two commands executed (successfully), while the last two were apparently never received by the server (or at least, never processed by Bash).

Best Answer

A quick and dirty solution is to run this:

bash -c '<paste commands here>'

This works even if the paste contains newline characters. It may fail if the paste contains single quotes. If you're aware of the bash quoting rules, you should be able to modify this method for the specific commands that you're trying to run.

Related Question