24

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

33

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]\
> ...
  • 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 8 at 15:02
12

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 \\
\
$
6

[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 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 help you either clearly see it is a multiline commands but disable it's multiline content (ex: PS2="#cont#") or let you easily copy/paste them with as little impact on its lines as possible (ex: PS2=" ")

Which, by the way, is imo a bad default, as it could very well lead one to delete some important binary commands in some cases.

You can 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 shifter 2 spaces to the right, and no ">" was 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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.