简体   繁体   English

bash或powershell中的>>模式是什么?

[英]What is the >> mode in bash or powershell?

If a single ` is entered in a command in bash or powershell, it enters a mode displaying >> on the prompt. 如果在bash或powershell中的命令中输入了一个` ,它将进入提示符下显示>>的模式。 What is this mode and what is it used for? 此模式是什么?它的作用是什么?

I typed cd ` instead of cd ~ and entered the mode. 我键入cd `而不是cd ~并进入模式。 The only input that seemed to affect it was ctrl + c to terminate the command. 似乎影响它的唯一输入是ctrl + c来终止命令。 I haven't been able to find anything regarding this searching the bash man pages or reference manual. 关于搜索bash手册页或参考手册,我什么也找不到。

In bash/sh the ` character starts Command Substitution . 在bash / sh中, `字符启动Command Substitution

When you didn't finish the command (with another ` character) the shell realizes your command is unfinished and attempts to prompt you for more (using the value of $PS2 ). 当您没有完成命令(带有另一个`字符)时,shell会意识到您的命令未完成,并尝试提示您更多命令(使用$PS2的值)。

Finish the command and hit enter and the entire thing will run. 完成命令,然后按Enter键,整个过程就会运行。

The same thing is true for unfinished strings ( ` and " ) as well. 未完成的字符串( ` )也是如此。

In powershell I believe ` is line continuation. 在powershell中,我相信`是行的延续。 (Similar to \\ in shell scripts and the like.) (类似于shell脚本等中的\\ 。)

As requested I am moving my comment to an answer to address the PowerShell side of the question. 根据要求,我将评论移至答案,以解决问题的PowerShell方面。

In PowerShell the backtick ` is the Escape character. 在PowerShell中,反引号`是转义字符。 For the purposes of the OP's question, and incurring a >> prompt, the backtick ` is escaping the New Line and forcing the command interpreter to continue the current command on to the next line. 就OP的问题而言,并引起>>提示,反引号`逃避了新行,并强制命令解释器将当前命令继续到下一行。 So when the last character in the line is a ` it functions as Etan suggested as a line continuation character. 因此,当行中的最后一个字符是`时,它的作用就像Etan建议的那样作为行继续符。 When he hit Enter immediately after the backtick it gave the >> expecting him to finish the current command he was working on. 当他在反引号之后立即按Enter时,它给出>>期望他完成他正在处理的当前命令。

If not the last character it escapes whatever it precedes, allowing people to escape quotes within quotes, or state variable names within double quotes without string interpolation. 如果不是最后一个字符,则它会转义其后面的所有字符,从而使人们能够转义引号内的引号,或在双引号内声明状态变量名称而无需字符串内插。

The >> prompt is, as explained in other answers, the host waiting for you to complete something. 如其他答案所述, >>提示符是主机等待您完成某些操作的主机。 Be it a command, a string, a scriptblock, or a loop or some such. 它可以是命令,字符串,脚本块或循环等。

(thank you Etan for indirectly showing me how the ` thing is done btw, that's kind of awesome for answering things here) (感谢Etan间接向我展示`事情是如何完成的,在这里回答问题真是太棒了)

Edit: Bah, Keith Hill wandered in and helped me stick my foot in my mouth. 编辑:B ,基思·希尔(Keith Hill)走进来,帮助我将脚伸进嘴里。 I'll stand by my answer, as I believe it to be functionally correct (if not technically thorough), but evidently it is referred to as the line continuation character in documentation. 我坚持我的回答,因为我认为它在功能上是正确的(如果在技术上不够彻底),但是显然,它在文档中被称为line continuation

A backtick ( ` ) begins a quote context and will keep reading until ended with another matching ` . 反引号( ` )开始引用上下文,并将继续阅读直到以另一个匹配的`结尾。 This mode is entered/continued when a quote is not completed on the current line (eg when enter is pressed). 当当前行的引号未完成时(例如,按Enter键时),将进入/继续此模式。

Unlike with the ' and " quote contexts it also expects inner ( ' and " ) quotes to be terminated correctly before it will terminate the ">>" context. '"引用上下文不同,它还希望内部( '" )引号在终止“ >>”上下文之前正确终止。 (It actually doesn't matter that this mode is entered, that is just the shell saying that the expression has not been correctly terminated when run interactively.) (实际上,进入此模式并不重要,只是shell表示在交互运行时表达式未正确终止。)

Consider this terminating input (it runs uname, capturing the output, and then displays it with echo): 考虑以下终止输入(它运行时不同步,捕获输出,然后显示回显):

echo "Hello " `
uname -m` ", you are awesome!"

And this this non-terminating (as the inner " is not closed) input: 这个非终止的(因为内部的"未关闭”)输入:

echo `
echo "Hello world
`

And with standard (non-substitution quotes): 并使用标准(非替代引号):

echo "Hello
word `"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM