如何将多行bash代码粘贴到终端并一次运行?

时间:2021-06-23 02:04:20

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.

我需要将多行bash代码粘贴到终端中,但是每当我这样做的时候,每一行都会在被粘贴时作为一个单独的命令运行。

9 个解决方案

#1


42  

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

在复制之前,试着把\放在每一行的末尾。

#2


12  

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:

如果您想从网站/测试编辑器/等等粘贴多行到bash中,不管它是每行命令还是函数或整个脚本……只需以a开头(以a结尾),然后输入,如下例所示:

If I had the following blob

如果我有下面这个团

function hello {
    echo Hello!
}
hello

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

您可以使用bash by在终端中粘贴和验证:

1) Starting with (

1)开始(

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

2)粘贴文本,按Enter键(使其美观)……或不

3) Ending with a ) and pressing Enter

3)以a结尾),按回车键

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!

粘贴的文本会自动地继续,每一行都有一个预挂的>。我用多行测试了每一行、函数和整个脚本的命令。希望这能帮助别人节省一些时间!

#3


11  

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.

如果您按C-x C-e命令,将打开定义.bashrc的默认编辑器,然后您就可以使用编辑器的所有强大功能。保存和退出时,行将等待输入。

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

如果您想定义您的编辑器,只需在~/.bashrc中为Ex. editor =emacs -nw或editor =vi编写

#4


10  

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

除了反斜杠,如果一行以|或&或||结尾,它将在下一行继续。

#5


2  

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

为了防止文本文件中的长行命令,我保留了类似这样的复制-pase片段:

echo a;\
echo b;\
echo c

#6


1  

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
)

#7


0  

Try this way:

试试这种方法:

echo $( 
    cmd1
    cmd2
    ...
)

#8


0  

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

iTerm很好地处理多行命令,它将多行命令保存为一个命令,然后我们可以使用Cmd+Shift+;浏览历史。检查更多的迭代技巧,以有效地使用迭代

#9


-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.

然后粘贴您的行并按Ctrl-D(插入EOF字符)。所有输入到Ctrl-D将被重定向到cat的stdout。

#1


42  

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

在复制之前,试着把\放在每一行的末尾。

#2


12  

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:

如果您想从网站/测试编辑器/等等粘贴多行到bash中,不管它是每行命令还是函数或整个脚本……只需以a开头(以a结尾),然后输入,如下例所示:

If I had the following blob

如果我有下面这个团

function hello {
    echo Hello!
}
hello

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

您可以使用bash by在终端中粘贴和验证:

1) Starting with (

1)开始(

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

2)粘贴文本,按Enter键(使其美观)……或不

3) Ending with a ) and pressing Enter

3)以a结尾),按回车键

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!

粘贴的文本会自动地继续,每一行都有一个预挂的>。我用多行测试了每一行、函数和整个脚本的命令。希望这能帮助别人节省一些时间!

#3


11  

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.

如果您按C-x C-e命令,将打开定义.bashrc的默认编辑器,然后您就可以使用编辑器的所有强大功能。保存和退出时,行将等待输入。

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

如果您想定义您的编辑器,只需在~/.bashrc中为Ex. editor =emacs -nw或editor =vi编写

#4


10  

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

除了反斜杠,如果一行以|或&或||结尾,它将在下一行继续。

#5


2  

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

为了防止文本文件中的长行命令,我保留了类似这样的复制-pase片段:

echo a;\
echo b;\
echo c

#6


1  

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
)

#7


0  

Try this way:

试试这种方法:

echo $( 
    cmd1
    cmd2
    ...
)

#8


0  

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

iTerm很好地处理多行命令,它将多行命令保存为一个命令,然后我们可以使用Cmd+Shift+;浏览历史。检查更多的迭代技巧,以有效地使用迭代

#9


-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.

然后粘贴您的行并按Ctrl-D(插入EOF字符)。所有输入到Ctrl-D将被重定向到cat的stdout。