多行字符串额外的空间(preserved缩进) [英] Multi-line string with extra space (preserved indentation)

查看:92
本文介绍了多行字符串额外的空间(preserved缩进)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一些pre定义的文本与以下文件:

I want to write some pre-defined texts to a file with the following:

text="this is line one\n
this is line two\n
this is line three"

echo -e $text > filename

我期待这样的事情:

I'm expecting something like this:

this is line one
this is line two
this is line three

但得到这个:

this is line one
 this is line two
 this is line three

我敢肯定有每个后面没有空格 \\ n ,但如何额外的空间出来?

I'm positive that there is no space after each \n, but how does the extra space come out?

推荐答案

定界符听起来为此更方便。它主要用于将多个命令发送到命令间preTER程序行的

Heredoc sounds more convenient for this purpose. It is used to send multiple commands to a command interpreter program line ex

cat << EndOfMessage
This is line 1.
This is line 2.
Line 3.
EndOfMessage

后面的字符串'&LT;&LT;'指出在哪里停下来。

The string after '<<' indicates where to stop.

要这些行发送到一个文件,使用:

To send these lines to a file, use:

cat > $FILE <<- EOM
Line 1.
Line 2.
EOM

您也可以这些行存储到一个变量:

You could also store these lines to a variable:

read -r -d '' VARIABLE << EOM
This is line 1.
This is line 2.
Line 3.
EOM

该存储行变量命名变量。

This stores the lines to the variable named VARIABLE.

记住周围的变量引号,否则你将看不到换行符。

Remember the quotes around the variable otherwise you won't see the newline characters.

echo "$VAR"

更妙的是,你可以用缩进,使其在code的脱颖而出。 ';&LT;&LT'之后 - 这一次,只需添加一个'阻止出现的选项卡。

Even better, you can use indentation to make it more stand out in your code. This time just add a '-' after '<<' to stop the tabs from appearing.

read -r -d '' VARIABLE <<- EOM
    This is line 1.
    This is line 2.
    Line 3.
EOM

不过,你必须使用制表符而不是空格进行缩进。

But then you must use tabs not spaces for indentation.

这篇关于多行字符串额外的空间(preserved缩进)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆