How do I paste multi-line bash codes into terminal

2020-02-16 06:49发布

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.

10条回答
Bombasti
2楼-- · 2020-02-16 07:05

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

查看更多
何必那么认真
3楼-- · 2020-02-16 07:08

Another possibility:

bash << EOF
echo "Hello"
echo "World"
EOF
查看更多
淡お忘
4楼-- · 2020-02-16 07:14

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

查看更多
\"骚年 ilove
5楼-- · 2020-02-16 07:15

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.

查看更多
太酷不给撩
6楼-- · 2020-02-16 07:20

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/test 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!

查看更多
太酷不给撩
7楼-- · 2020-02-16 07: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
)
查看更多
登录 后发表回答