28

When I press backslash \, I’m given a > (greater than) symbol. What does this mean?

Melebius
  • 11,431
  • 9
  • 52
  • 78

3 Answers3

38

Whenever you use the command line, there might be an instance when you need to run a command that is very long. So, you want to split the command into multiple lines for better readability and understanding. But if you use new line character which is typed by Enter, the shell will think that it is a new command. So you use \ followed by the newline character.

Basically, commands or bash scripts are "interpreted", i.e. executed line by line. Every new line means the start of a new command. In the terminal, when you press Enter, you get a prompt to run a new command. So, a new line needs to be "escaped". Typing \ followed by Enter allows you to split the current command into multiple lines so the shell doesn't think that it is a new command but the continuation of the previous command.

> is nothing but a prompt for the next line of the command being entered.

For example:
If we want to install multiple packages, the command will be like

$ sudo apt install [package1] [package2] [package3] ...

But sometimes, that makes the command cluttered. So we can use \ followed by Enter (newline character)

$ sudo apt install [package1]\
> [package2]\
> [package3]\
> ...
wjandrea
  • 14,236
  • 4
  • 48
  • 98
Kulfy
  • 17,696
  • 4
    @Curiouskangaroo BTW, you can control how the new command and line continuation prompts work, by setting values to the $PS1 and $PS2 variables. See the bash man page to learn the format, and use echo "'$PS2'" to view the current setting. – jpaugh Jan 08 '19 at 15:02
  • that's great but how do i end it? Ctrl Enter doesnt work ; doesnt work, Shift Enter doesn't work. only Ctrl +C works with doesn't run the multiline command – Simple Fellow Dec 20 '21 at 04:24
  • @SimpleFellow I'm not sure which command you're running, however if you're using quotes (' or "), since they work in pairs, you need to use a closing quote as well. In case you're using backslash, you need to complete the command. You may try entering semicolon (;) after the command. Also, Ctrl+C sends SIGINT and doesn't help in executing commands – Kulfy Jan 22 '22 at 15:05
16

The backslash character (\) is used as an escape character in the shell. If you use it as the last character on the line, it escapes the newline, so you can continue your command on the next line instead of finishing it. This is indicated by the > prompt in Bash.

Example:

$ echo A\
> B
AB
$

To put a literal \ to your command, you have to escape it using another backslash:

$ echo \\
\
$
Melebius
  • 11,431
  • 9
  • 52
  • 78
9

[adding a (too long/complex) answer as the other 2 don't mention how the "> " appears... ie, don't mention PS2]

You typed: \ [Enter]

The \ says to the shell to just output the [Enter] as a literral newline character instead of interpreting it as usual (Therefore the shell "goes to the next line" instead of terminating the current command line and interpreting it. Unless you are in some other constructs such as an heredoc, a for loop, etc).

Your terminal therefore interprets: \ [Enter] as : "go to the next line" (without starting to interpret the command) and thus the terminal is now letting you enter the 2nd line of a multi-line command, and to make it more visible displays the $PS2 content (called the PS2 prompt) on each subsequent line.

The PS2 variable is usually defined by default as : PS2="> " and you can for exemple edit your ~/.bashrc to redefine it as you wish (taking into consideration that it should, imo, avoid containing dangerous characters, such as > or ;, and should instead help you either clearly see that it is a multiline commands but disable it's multiline content (ex: PS2="#cont# ") or be as "transparent" as possible and let you easily copy/paste multiline commands with as little impact on its lines as possible (ex: PS2=" ", or even PS2="")

The default of PS2="> " is, in my opinion, a bad one : it could lead to you copy-pasting a multiline command that may, depending on where the lines are cut at, end up with lines containing `> somethingimportant', possible cloberring a file or worse...

You can (and maybe should) redefine PS2 to be something else (I like: PS2=" ", for exemple) so that multiline commands can be easily copied/pasted without fearing the following:

For exemple let's say you have a command that starts to be quite long (and may fold on your screen if your terminal isn't wide enough):

grep -i "something"  /some/file  /another/file /3rd/file /etc/someimportantfile 

If the command looks too long (and wraps around), you may want to split it visually into 2 lines, by choosing where (when) you want to the next line by inserting: \ [Enter] at the appropriate spot:

grep -i "something"  /some/file  /another/file /3rd/file \
> /etc/someimportantfile #warning, "> " was inserted by the shell and this changes everything !

Using the default PS2, the shell added > before "/etc/someimportantfile " .. so if you copy/paste those 2 lines in another terminal, their action will be completely different: instead of grepping into 4 files, the grep is only going into the first 3 files, and the grep output replaces the content of the 4th file (/etc/someimportantfile) !

To avoid these problems (and many others) : you could for exemple define: PS2=" " to make the multi-line commands cleaner and easier to copy/paste:

grep -i "something"  /some/file  /another/file /3rd/file \
  /etc/someimportantfile #now only 2 spaces were inserted, without changing the grep's actions!

Note how this time /bin/somecommand is simply shifted 2 spaces to the right, and no > were inserted, so you can safely copy/paste this 2-line command.

PS2 is also used in "for" "while" etc loops, and having it defined as " " is, to me, better in those ones too.

PS2="" is even more transparent, and let you choose the identation you want to use on every subsequent lines.