Linux 如何将多行 bash 代码粘贴到终端并一次运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6713056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I paste multi-line bash codes into terminal and run it all at once?
提问by Nathan
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 代码粘贴到终端中,但是每当我这样做时,每一行都会在粘贴后立即作为单独的命令运行。
采纳答案by opsguy
Try putting \at the end of each line before copying it.
\在复制之前尝试将其放在每行的末尾。
回答by glenn Hymanman
In addition to backslash, if a line ends with |or &&or ||, it will be continued on the next line.
除了反斜杠,如果一行以|or&&或结尾||,它将在下一行继续。
回答by nvd
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
回答by sspprroo
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 的标准输出。
回答by itirazimvar
If you press C-x C-ecommand 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 -nwor EDITOR=viinside of ~/.bashrc
如果您想定义您的编辑器,只需为 Ex 编写。EDITOR=emacs -nw或EDITOR=vi里面~/.bashrc
回答by TryTryAgain
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)和 Enter结尾,例如在以下示例中:
If I had the following blob
如果我有以下 blob
function hello {
echo Hello!
}
hello
You can paste and verify in a terminal using bash by:
您可以通过以下方式使用 bash 在终端中粘贴和验证:
Starting with
(Pasting your text, and pressing Enter (to make it pretty)... or not
Ending with a
)and pressing Enter
从...开始
(粘贴您的文本,然后按 Enter(使其变得漂亮)...或不
以 a 结尾
)并按 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!
粘贴的文本会自动继续,并>为每一行添加前缀。我已经用多行测试了每行命令、函数和整个脚本。希望这可以帮助其他人节省一些时间!
回答by abrkn
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
)
回答by voltento
Try this way:
试试这个方法:
echo $(
cmd1
cmd2
...
)
回答by jeffery.yuan
iTerm handles multiple-line command perfectly, it saves multiple-lines command as one command, then we can use Cmd+ Shift+ ;to navigate the history.
完美的iTerm处理多行命令,这样可以节省多行命令一个命令,那么我们可以用Cmd+ Shift+;浏览历史。
Check more iTerm tips at Working effectively with iTerm
在使用 iTerm 有效工作中 查看更多 iTerm 提示
回答by Lenar Hoyt
Another possibility:
另一种可能:
bash << EOF
echo "Hello"
echo "World"
EOF

