- 1 year ago
Writing to standard output
To write to the standard output in a Windows Command Prompt (cmd), you can use the echo
command or redirect output using the >
or >>
operators. Here are a few examples:
Using the
echo
command:bashecho Hello, World!
This will output "Hello, World!" to the standard output.
Redirecting output using the
>
operator:bashecho Output to file > output.txt
This will write the text "Output to file" to a file named "output.txt" in the current directory.
Appending output to a file using the
>>
operator:luaecho More output >> output.txt
This will append the text "More output" to the end of the "output.txt" file.
Note that in the examples above, the echo
command is used to demonstrate writing to the standard output. However, you can also use other commands or programs to generate output that will be displayed in the Command Prompt window by default.
If you want to capture the standard output of a command or program and store it in a variable or use it in a script, you can use the FOR /F
command in combination with command substitution. Here's an example:
bashFOR /F "delims=" %%G IN ('dir /B') DO (
echo %%G
)
In this example, the dir /B
command lists the files and directories in the current directory, and the FOR /F
loop captures each entry and echoes it to the standard output.