Linux 如何将回车作为字符添加到文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7742340/
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-08-05 06:39:12  来源:igfitidea点击:

How to add a carriage return as a character to a file?

linuxunixspecial-characters

提问by wonnie

I wanna have a string like:

我想要一个像这样的字符串:

blablbabla<carriage return goes here>

I mean the string should contain the carriage return. Is there any simple way to do it? Or if a write a program in c and use fputs with blablabla\r, does this do the trick?

我的意思是字符串应该包含回车符。有什么简单的方法可以做到吗?或者,如果 a 用 c 编写程序并使用 fputs with blablabla\r,这能解决问题吗?

采纳答案by Jeff Ferland

If you're using vimyou can enter insert mode and type 'CTRL-v CTRL-m'. That ^M is the keyboard equivalent to \r. (see Insert the carriage return character in vim)

如果您正在使用,vim您可以进入插入模式并键入“CTRL-v CTRL-m”。^M 是等效于 \r 的键盘。(请参阅在 vim 中插入回车符

Inserting 0x0D in a hex editor will do the task.

在十六进制编辑器中插入 0x0D 将完成任务。

回答by Alexander Pogrebnyak

echo -ne "blablbabla\r" > /path/to/your/file

echo -ne "blablbabla\r" > /path/to/your/file

回答by mkj

SHELLVAR=$(echo -ne "blablabla\r")

See the echoman page — the -eoption causes echo to interpret backslash escapes.

请参阅echo手册页 - 该-e选项会导致 echo 解释反斜杠转义。

Also, at least in bash's normal edit mode, the same control-v control-m sequence works to insert a carriage return character literally; it will show as ^M

此外,至少在 bash 的正常编辑模式下,相同的 control-v control-m 序列可以按字面意思插入回车符;它会显示为^M