Search code examples
windowscmdechoio-redirection

How can I echo a string that ends with a digit?


In Windows command line, I'm trying to write the following string to a file:

cloud 9

First, I try echo cloud 9> file.txt. This just prints to the console, not the file, presumably due to the undefined nature of 9>.

Next I try echo cloud 9 > file.txt. This yields a file that contains the desired text cloud 9, but with a trailing space at the end of the line.

I tried a bunch of other variations:

echo cloud 91> file.txt yields cloud 91.

echo cloud 9 1> file.txt yields cloud 9 with a trailing space.

echo "cloud 9"> file.txt yields "cloud 9" with undesired quotation marks.

Is there any way to echo exactly cloud 9, with no trailing spaces, to a file?


Solution

  • I/O redirections don't have to be at the end of the command. Try

    echo>file.txt cloud 9
    

    or

    >file.txt echo cloud 9
    

    Other possible alternatives:

    escape the 9:

    echo cloud ^9>file.txt
    

    surround the text with parentheses: (note that any interior closing-parentheses will need to be escaped)

    (echo cloud 9) > file.txt