How do I paste multi-line bash codes into terminal and run it all at once?

128,120

Solution 1

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.

Solution 2

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!

Solution 3

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

Solution 4

Another possibility:

bash << EOF
echo "Hello"
echo "World"
EOF

Solution 5

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

Share:
128,120
Admin
Author by

Admin

Updated on February 13, 2022

Comments

  • Admin
    Admin about 2 years

    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.

  • Mohamed Bana
    Mohamed Bana about 7 years
    try this: $ out=$(cat) && eval "$out". hit enter after last line then CTRL+D as suggested above.
  • mklement0
    mklement0 about 7 years
    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.
  • Hobroker
    Hobroker almost 7 years
    easiest method IMO
  • GypsyCosmonaut
    GypsyCosmonaut almost 7 years
    Do note, like paranthesis (, braces { would also work the same in the above example
  • TryTryAgain
    TryTryAgain over 6 years
    @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.)
  • tripleee
    tripleee over 6 years
    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
    tripleee over 6 years
    This seems to duplicate an earlier answer on this page
  • Max Reeder
    Max Reeder almost 6 years
    Note: trailing spaces will break it!
  • al-ash
    al-ash over 3 years
    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?
  • eltbus
    eltbus over 2 years
    My favored method. Also, check out this answer to see how to pipe/redirect when using heredocs