![](/img/trans.png)
[英]The ECHO command echo only show the last line of my file instead of shows each line
[英]CMD Echo Command Keeps Only Last Line of Text
我正在創建一個批處理文件,我遇到以下命令時遇到問題:
echo Hello & echo.World > Text.txt
每當我執行此命令時,輸出僅顯示最后一行,如下所示:
世界
但是,如果我只是在CMD中執行上述命令而不寫入文件,它在命令提示符中就會完美顯示。 我可以完全訪問我的文件,但我似乎仍然無法編寫整個文本。
在批處理中, >
運算符的優先級高於&
運算符,因此您的示例僅將最后一個命令echo.World
重定向到該文件。 第一個命令仍在輸出到控制台。
要重定向多個命令,可以用括號括起它們。
(echo Hello & echo.World) > Text.txt
批處理&是一個命令分隔符。 您可以使用兩個單獨的echo
線生成相同的結果。
echo Hello&echo.World> Text.txt
作為兩個獨立的命令執行,
echo Hello echo.World> Text.txt
它會向控制台報告Hello
並將World
寫入文件(你沒有提到Hello
出現在控制台上......)
請注意>>創建一個新文件,而>> 附加到任何現有文件(或創建一個新文件)
echo Hello > Text.txt
echo.World > Text.txt
首先將Hello
寫入新文件,然后用World
覆蓋它。
要附加第二行,您需要
echo Hello > Text.txt
echo.World >> Text.txt
或者,在一條線上,
echo Hello > Text.txt&echo.World >> Text.txt
帶括號的語句序列(又稱“塊”)被視為一個復雜語句,因此
(echo Hello &echo.World)> Text.txt
會創建一個包含兩行的新文件,而
(echo Hello &echo.World)>> Text.txt
將兩行附加到任何現有文件。
另請注意, >之前的尾隨空格也將寫入文件。
關於>和>>要非常小心。 如果你執行的命令是
echo Hello 1> Text.txt
然后將hello Space寫入文件。
如果以空格終止的消息將一個數字寫入文件,那么很可能不會出現任何內容。 當我們遇到這種情況時,緊接在>確定output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
“邏輯設備output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
”之前的數字output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
2> errorreports.txt` output will be used. The useful ones here are 1 (standard output) and 2(standard error) so you can redirect errormessages using
消息。
如果您遇到這種情況,可以通過使用來克服這一特性
> Text.txt echo Hello 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.