139

I need to paste a multi-line bash code into terminal, but whenever I do, each line gets run as a separate command as soon as it gets pasted.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Nathan
  • 1,393
  • 2
  • 9
  • 4

13 Answers13

135

Try putting \ at the end of each line before copying it.

Example:

echo "Hello world" && \
script_b.sh

echo $?

The exit code ($?) is now the full sequence of commands, and not just the last command.

Melroy van den Berg
  • 2,697
  • 28
  • 31
opsguy
  • 1,781
  • 1
  • 12
  • 11
96

I'm really surprised this answer isn't offered here, I was in search of a solution to this question and I think this is the easiest approach, and more flexible/forgiving...

If you'd like to paste multiple lines from a website/text editor/etc., into bash, regardless of whether it's commands per line or a function or entire script... simply start with a ( and end with a ) and Enter, like in the following example:

If I had the following blob

function hello {
    echo Hello!
}
hello

You can paste and verify in a terminal using bash by:

  1. Starting with (

  2. Pasting your text, and pressing Enter (to make it pretty)... or not

  3. Ending with a ) and pressing Enter

Example:

imac:~ home$ ( function hello {
>     echo Hello!
> }
> hello
> )
Hello!
imac:~ home$ 

The pasted text automatically gets continued with a prepending > for each line. I've tested with multiple lines with commands per line, functions and entire scripts. Hope this helps others save some time!

TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
  • 6
    easiest method IMO – Hobroker Jun 25 '17 at 03:16
  • 1
    @Toolkit for the simple things it will work as you described, but the solution I've offered will work for more complex situations (line breaks, multiple functions, entire scripts, etc.) – TryTryAgain Jan 09 '18 at 17:52
  • 1
    This runs the pasted commands in a subshell, so it often doesn't actually do what you want. You can use braces instead of parentheses to force it to run in the current shell; but really, just don't do either. The shell can cope. – tripleee Jan 31 '18 at 10:26
  • I also really like this option. – darul75 Sep 15 '22 at 06:51
47

If you press C-x C-e command that will open your default editor which defined .bashrc, after that you can use all powerful features of your editor. When you save and exit, the lines will wait your enter.

If you want to define your editor, just write for Ex. EDITOR=emacs -nw or EDITOR=vi inside of ~/.bashrc

mklement0
  • 382,024
  • 64
  • 607
  • 775
itirazimvar
  • 859
  • 1
  • 10
  • 20
  • 3
    The technique is useful, but note that the editor must run _synchronously_ (as `emacs` and `vi` do), and on saving and exiting the commands are executed _instantly_. – mklement0 Apr 02 '17 at 03:28
20

Add parenthesis around the lines. Example:

$ (
sudo apt-get update
dokku apps
dokku ps:stop APP # repeat to shut down each running app
sudo apt-get install -qq -y dokku herokuish sshcommand plugn
dokku ps:rebuildall # rebuilds all applications
)
abrkn
  • 1,971
  • 2
  • 15
  • 16
19

Another possibility:

bash << EOF
echo "Hello"
echo "World"
EOF
Lenar Hoyt
  • 5,971
  • 6
  • 49
  • 59
  • My favored method. Also, check out [this answer](https://unix.stackexchange.com/a/547990/356252) to see how to pipe/redirect when using heredocs – eltbus Oct 08 '21 at 07:48
19

In addition to backslash, if a line ends with | or && or ||, it will be continued on the next line.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
7

To prevent a long line of commands in a text file, I keep my copy-pase snippets like this:

echo a;\
echo b;\
echo c
Gajus
  • 69,002
  • 70
  • 275
  • 438
nvd
  • 2,995
  • 28
  • 16
  • This answer seems to achieve the same as accepted answer with just 2 instead of 3 (&&\) characters. Why is the accepted answer so much preferable? – al-ash Nov 19 '20 at 18:27
2

iTerm handles multiple-line command perfectly, it saves multiple-lines command as one command, then we can use Cmd+ Shift + ; to navigate the history.

Check more iTerm tips at Working effectively with iTerm

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
jeffery.yuan
  • 1,177
  • 1
  • 17
  • 27
2

I'm surprised no one's said this, but you can use fc to paste all the commands in an editor and run them at once

Patrick Stetz
  • 455
  • 2
  • 7
  • 14
2

So I think for very long command (namely compiling a file with llvm!) this has given me the most ease of us over the others Ive tried in the past. I use it all the time to thrash out a command in an editor over multiple lines:

cat | paste -sd " "  - | xargs echo

This will open up cat standard input in the terminal, paste in the code and follow up with ctrl-d (mentioned before this adds a EOF char and ends CAT process). Paste takes in the cat input and scoops up all multi lines into a space char. This will ONLY echo out the command.

To execute the command just add sh to the end:

cat | paste -sd " "  - | xargs echo | sh

Or alternatively I like to make the output an alias command, place in .zshrc

alias mcmd='cat | paste -sd " "  - | xargs echo'

then to execute

> mcmd | sh

this will open cat, paste in your multi line command, ctrl d and will execute the multi lines as a one line command :)

Example command

>    mcmd;
# now paste in your long command
> llvm-g++
    main.mm
    -o
    buildApp
    -w
    -g
    -std=c++14
    -L/System/Library/Frameworks/
    -framework
    CoreServices
     # now key press ctrl + d

whoop! outputs single line

 llvm-g++ main.mm -o buildApp -w -g -std=c++14 -L/System/Library/Frameworks/ -framework CoreServices
  • This helped me - I needed to paste a long list of arguments for a single command. I think there's other ways to do it, but this is useful for what I needed. – Mat Carlson Dec 19 '22 at 18:57
1

Try this way:

echo $( 
    cmd1
    cmd2
    ...
)
voltento
  • 833
  • 10
  • 26
-1

Try

out=$(cat)

Then paste your lines and press Ctrl-D (insert EOF character). All input till Ctrl-D will be redirected to cat's stdout.

sspprroo
  • 41
  • 1
  • 1
  • 4
-1

I have vi key bindings set in my shell. I don't recall, offhand, how I configured this. But when entering a command, I can switch from editing into command mode, type v and have the command automatically pop up in a vim session. Exiting the session (:wq) submits the command to bash for execution.

Tom Russell
  • 1,015
  • 2
  • 10
  • 29