How to echo "2" (no quotes) to a file, from a batch script?

WindowsBatch FileCmdEchoIo Redirection

Windows Problem Overview


How do I echo the number 2 into a file, from a batch script?

This doesn't work:

Echo 2>> file.txt

because 2>> is a special command. :(

Windows Solutions


Solution 1 - Windows

Little-known feature: The redirection operator can go anywhere on the line.

>>file.txt echo 2

Solution 2 - Windows

Use (ECHO 2)>>file.txt. This will output 2 without any spaces.

Solution 3 - Windows

echo ^2>>file.txt appears to work for me.

Solution 4 - Windows

Use the ^ escape :

Echo ^2>> file.txt

Solution 5 - Windows

echo.2>>text.txt

Yes, it's weird.

Solution 6 - Windows

another method

echo>>file.txt 2

Solution 7 - Windows

Or you can use the delayed expansion

setlocal EnableDelayedExpansion
set "var=2"
echo(!var!>> file.txt

Solution 8 - Windows

If you need an accurate write, use this command

(ECHO |set /p=2)>>file.txt

the command by DMan will produce a 0x0D0A line break behind the "2"

Solution 9 - Windows

To write a number to a file without a trailing line-break you could use the following:

cmd /C set /A "2">file.txt

This works, because set /A returns the (last) result (without line-break) when executed in command prompt context (hence when directly run in cmd.exe).

If you are working in command prompt anyway, you could simply use this:

set /A "2">file.txt

Though you cannot use that in a batch file, you need the extra cmd /C to force the right execution context.

Needless to say, this can of course only output numbers, or precisely said, signed 32-bit integers.

Solution 10 - Windows

Based on this answer, if anyone is wondering how to do this:

echo "command1 && command2" > file.txt

with no qoutes in file.txt then you do it like this:

echo command1 ^&^& command2 > file.txt

Tested on Windows Server 2003.

Solution 11 - Windows

escape the haracter '2'

in linux: echo \2>file.txt

in windows: echo ^2>file.txt

in windows this also will work

echo.2>file.txt no space between echo and the dot

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser541686View Question on Stackoverflow
Solution 1 - WindowsRaymond ChenView Answer on Stackoverflow
Solution 2 - WindowsDominic KView Answer on Stackoverflow
Solution 3 - WindowslavinioView Answer on Stackoverflow
Solution 4 - WindowsMoe SiskoView Answer on Stackoverflow
Solution 5 - WindowsKornel KisielewiczView Answer on Stackoverflow
Solution 6 - Windowswalid2miView Answer on Stackoverflow
Solution 7 - WindowsjebView Answer on Stackoverflow
Solution 8 - Windowsminhng99View Answer on Stackoverflow
Solution 9 - WindowsaschipflView Answer on Stackoverflow
Solution 10 - WindowsJan ŚwięckiView Answer on Stackoverflow
Solution 11 - WindowsmilevyoView Answer on Stackoverflow