Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 556
Overlord Zurg Avatar asked Oct 29 '25 14:10

Overlord Zurg


2 Answers

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
like image 98
melpomene Avatar answered Oct 31 '25 05:10

melpomene


My preferred method is to put the redirection portion in front of the command:

> "file.txt" echo cloud 9

Anyway, you may hide the echoed text with the number in a variable. Though take into account that immediate variable expansion, or percent-(%-)expansion, happens even before redirection expressions are recognised, so set "CL=cloud 9" and then echo %CL%> "file.txt" is not going to work. But there are some other ways:

  1. Use delayed expansion (enable it by running the command prompt with cmd /V):

    set "CL=cloud 9"
    echo !CL!> "file.txt"
    

    Or in a single line:

    set "CL=cloud 9" & echo !CL!> "file.txt"
    

    This may cause trouble when the text string contains ! and/or ^.

  2. Use delayed expansion later, but have it disabled initially:

    set "CL=cloud 9"
    cmd /V /C echo !CL!> "file.txt"
    

    Or in a single line:

    set "CL=cloud 9" & cmd /V /C echo !CL!> "file.txt"
    

    This is expected to work even with ! and/or ^ occurring in the text string.

    If delayed expansion is already enabled from the beginning, cmd /V /C echo !CL! needs to be replaced by cmd /V /C echo ^^!CL^^!.

  3. Use call to introduce another delayed layer of variable expansion:

    set "CL=cloud 9"
    call echo %^CL%> "file.txt"
    

    Or again in one line:

    set "CL=cloud 9" & call echo %^CL%> "file.txt"
    

    This may cause trouble when the text string contains ^. And actually this relies on the fact that there is no variable ^CL defined.

  4. Use a for loop loop to achieve later expansion of the meta-variable:

    for %I in ("cloud 9") do @echo %~I> "file.txt"
    

    This may cause trouble when the text string contains *, ? or <, >.


By the way, you may want to replace echo Text by echo(Text, because this is the only safe way of echoing an arbitrary text when Text is unknown and may even be blank, according to this post on DosTips.com.

like image 45
aschipfl Avatar answered Oct 31 '25 05:10

aschipfl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!