cmd.exe quoted string expansion?

Posted on


Bash has the feature of quoted string expansion, which will allow you to send special characters like so

  -r rtmp:// 
  -y tvnetwork-hellenictv-sigma-hsslive-25f-4x3-SDh$'r' 
  -o out.flv

In this case a literal carriage return being passed. The question is, how would you go about sending a literal carriage return with cmd.exe?


Windows CMD.EXE does not have a built in facility to work with special characters like linefeed or carriage return. But there are known hacks that can give you access to a few frequently needed characters.

CarriageReturn (CR) 0x0D

All that is needed is a file – any file. Preferably the file should be small so that the command runs faster. The contents of the file do not matter. It could even be empty!

The COPY /Z command prints a percent completion status without LF that leads with a CR repeatedly until it reaches 100%. A properly formed FOR statement can parse out the CR from the beginning of the line. You can then use the FOR variable in your statement.

D:test>for /f "delims=1 " %A in ('copy /z test.txt nul') do @echo Good morning %A world!

In the above, ” world!” over-writes the beginning of “Good morning” because of the CR contained in the %A variable.

The CR could be placed in an environment variable so that it can be used later on, but the CR will only be preserved if delayed expansion is used. Delayed expansion can be enabled by using cmd /v:on.

D:test>cmd /v:on
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:test>for /f "delims=1 " %A in ('copy /z test.txt nul') do set "CR=%A"

" test>set "CR=

D:test>echo Good morning !CR! world!

D:test>echo Good morning %CR% world! does not preserve the CR
Good morning  world! does not preserve the CR

LineFeed (LF) 0x0A

An LF can be incorporated into a command directly by using line continuation and entering a blank line to represent the escaped LF, followed by the remainder of the line.

D:test>echo hello^
More? world!

The LF can easily be stored in a variable where it can be used later. You cannot simply use %LF%, but you can use ^%LF%%LF% to represent a single LF.

D:test>set LF=^

D:test>echo Hello^%LF%%LF%world!

Or delayed expansion can be used to conveniently access the LF variable

D:test>cmd /v:on
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:test>echo Hello!LF!world!

Or another variable can be defined that has the proper escape sequence built in so that the variable can be used directly with normal expansion.

D:test>set n=^^^%LF%%LF%^%LF%%LF%

D:test>echo Hello%n%world!

BackSpace (BS) 0x08

The prompt can be set to (BS)(space)(BS) by using $H, and a single BS can be parsed out using a FOR loop. The BS character can be stored in an environment variable to be used later.

D:test>for /f %A in ('prompt $h^&for %a in (1^) do rem') do @set "BS=%A"

D:test>echo ab_%BS%cd

Leave a Reply

Your email address will not be published.