追加在同一行 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11939474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:00:37  来源:igfitidea点击:

Append on the same line bash

linuxbashappend

提问by dee doo

Using bash, I need to add to the end of a file, on the same line a string followed by the date. Example "Rebooted on Mon Aug 13 10:38:56 PDT 2012"

使用 bash,我需要在文件末尾添加一个字符串,后跟日期。示例“在 2012 年 8 月 13 日星期一 10:38:56 PDT 重新启动”

echo -n "Reboot done on " ; date

This gives me what I want on one line, but I can't append to the text file.

这给了我我想要的一行,但我不能附加到文本文件。

echo -n "Reboot done on " ; date > test.txt

This does not seem to work for me. It only gives me the date.

这似乎对我不起作用。它只给我日期。

Thanks in advance.

提前致谢。

回答by drjd

This is because your output Reboot done onwith echo and then you execute dateand put its output into the file. Try this instead:

这是因为您的输出Reboot使用 echo完成,然后您执行date并将其输出放入文件中。试试这个:

echo "Reboot done on $(date)" > test.txt

回答by Debaditya

Try this

尝试这个

echo -n "Reboot done on `date`" > test.txt

回答by Aoife

Here's another way:

这是另一种方式:

(echo -n "Reboot done on "; date) > test.txt