As this answer states, somewhat cryptically, the full technical definition of how the caret (^
) works is as follows:
A caret at the end of the line:
- removes the newline that follows (e.g. turning 2 lines into 1)
- escapes the first character of the next line
- removes the caret symbol (
^
) you typed
Since the piping symbol |
is the first character in the next line, it will be escaped and thus lose its special meaning and just be interpreted as a text character.
Examples:
dir ^
/B
Turns into:
dir ^/B
The ^
character is the escape character. The forward slash /
carries no special meaning, so escaping it has no effect, making the command work as expected and effectively turning it into this:
dir /B
However, look at this:
dir ^
& echo hello
Turns into:
dir ^& echo hello
Which escapes the special &
character (which has the special meaning of executing the right command after the left one). You will get an error since all of & echo hello
will be sent as parameters to the dir
command.
Which means that something like this will work:
dir ^
& & echo hello
Turns into:
dir ^& & echo hello
The first &
is escaped, while the second &
is interpreted correctly. This will run dir ^&
, which sends &
(raw text character) as the directory parameter for dir
, and then run echo hello
because of the second &
character working as normal. dir ^&
will fail because "&" is no valid directory, and then echo hello
will print out "hello".
Returning to piping, let's look at the example you provided that's not working:
dir ^
| sort
Turns into:
dir ^| sort
The piping symbol |
will be escaped and not be interpreted as piping (it will appear as text character). And thus, "| sort" will be sent as parameters to the dir
command, which won't find the directory, and fail.
To solve this, as you already figured out, you need to add a space at the start of the next line.
This way, the space will be escaped, while the subsequent pipe symbol |
will be interpreted as normal:
dir ^
| sort
Turns into:
dir ^ | sort
Which is effectively:
dir | sort