4

I tried googleing this and searching on stack overflow but it hasn't returned any usefull results
What does the ^ symbol do it cmd and why is it asking me more?
More what? more info? more specificity? more roast duck?

a single entry of ^ plus hitting the entry key returned
two seperate line questions of
more?
more?

Why two?
while entering a double ^^ returns,
'^' is not recognized as an internal or external command
why is it also only returning a single carat?

in first scenario a single carat returns a NOT NULL or Undefined Value
While the Second is maybe undefined?

Very short question.
enter image description here

My Question maybe a duplicate of this one:
What does the single circumflex do in cmd
But still puzzled by the output of the last command within the screen shot.

Ryan Stone
  • 345
  • 4
  • 19
  • Does this answer your question? [What does the single circumflex in the windows cmd shell mean: More?](https://stackoverflow.com/questions/33946226/what-does-the-single-circumflex-in-the-windows-cmd-shell-mean-more) – Compo Oct 17 '20 at 15:15

1 Answers1

2

1st off the caret (^) is the escape character for cmd, when used as the last character on a line it removed the end of the line and appends the next line and escapes the 1st character

2ndly contrary to popular belief, you can write multi-line commands into the cmd prompt, normally this is done by wrapping the commands in parenthesis. eg:

c:\>@(
more? echo.hello
more? echo.there
more? )

---[output]---
hello
there

An alternative way is to do this is to use the caret at the end of the line.

When the last character on the 1st line is a caret, it means the end of line is not processed (technically its removed along with the caret), and the first character of the 2nd line will be escaped.

So, on the 2nd line, if you just hit "enter" (carriage return) it will be escaped, and then you get the 2nd more? because that line will now also.be appended to the 1st.

however you've only escaped that 2nd EOL, Which means this is a method to save a carriage return into a variable. However echoing the line using the variable only works if delayed expansion is enabled.

So For Example This at the CLI is just fine:

c:\>echo.this ^
more? and that

---[result]---
this and that

You can also use this for setting Variables at the CLI and not have the carriage return matter

c:\>Set var=Some ^
more? Text
c:\> echo %var%

---[result]---
Some Text

As mentioned if echoing, and ^ is the last character of the line and you press enter on the first more? you will still be able to write more to the line and have it show up including the carriage return. This works without delayed expansion.

c:\>echo.this .^
more?
more? and that

---[result]---
this .
and that

however while you can save a variable on the CLI as mentioned above the following behavior will show up when you try to echo it:

c:\>Set var=Some ^
more?
more? Text
c:\> echo %var%

---[result]---
Some

This is because saving the variable this way will work even without delayed expansion, however to echo the value through the EOL character you will need to have delayed expansion on.

However we Can see that it is stored with the carriage return using SET

c:\>Set var=Some ^
more?
more? Text
c:\> SET var

---[result]---
var=Some
Text

So you can still get it out with a FOR loop, or you will need to turn on delayed expansion, this can be done by starting CMD with the /V option.

So this simple for loop will work, but only when there are no spaces or commas in the string stored in the variable.

So this will work:

c:\Set var=This_Has_no...Spaces^
More?
More? In_it
c:\>for %A IN (%var%) DO @(ECHO.%~A)

---[result]---
This_Has_no...Spaces
In_it

But this isn't going to get what you want:

c:\>Set var=Some more text ^
more?
more? goes here
c:\>for %A IN (%var%) DO @(ECHO.%~A)

---[result]---
Some
more
text
goes
here

However, as mentioned, SET does show the carriage return so you could get both lines out using a FOR /F Loop:

c:\>Set var=Some more text ^
more?
more? goes here
c:\>for /F "Tokens=*" %A IN ('SET VAR') DO @(ECHO.%~A)

---[result]---
var=Some more text
goes here

As but now we still have var= at the beginning so we'd really prefer not to have that which means we need an additional carriage return at the beginning of the variable and to skip the 1st line, to get it like this:

c:\>Set var=^
More?
More? Some text here ^
More?
More? Also Here
c:\>for /F "Tokens=* SKIP=1" %A IN ('SET VAR') DO @(ECHO.%~A)

---[result]---
Some text here
Also Here

So the easiest way to accomplish this is instead to enable delayed expansion and use echo as you normally would:

c:\>Set var=Some ^
more?
more? Text
c:\>cmd /V
c:\> echo !var!

---[result]---
Some
Text
Ben Personick
  • 3,074
  • 1
  • 22
  • 29