简体   繁体   中英

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. What is this mode and what is it used for?

I typed cd ` instead of cd ~ and entered the mode. The only input that seemed to affect it was ctrl + c to terminate the command. I haven't been able to find anything regarding this searching the bash man pages or reference manual.

In bash/sh the ` character starts 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 ).

Finish the command and hit enter and the entire thing will run.

The same thing is true for unfinished strings ( ` and " ) as well.

In powershell I believe ` is line continuation. (Similar to \\ in shell scripts and the like.)

As requested I am moving my comment to an answer to address the PowerShell side of the question.

In PowerShell the backtick ` is the Escape character. 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. So when the last character in the line is a ` it functions as Etan suggested as a line continuation character. When he hit Enter immediately after the backtick it gave the >> expecting him to finish the current command he was working on.

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)

Edit: Bah, Keith Hill wandered in and helped me stick my foot in my mouth. 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.

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

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

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 `"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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