Linux 向 unix shell 变量添加换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9402961/
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
Adding newline characters to unix shell variables
提问by James P.
I have a variable in a shell script in which I'd like to format the data. The variable stores new data during every iteration of a loop. Each time the new data is stored, I'd like to insert a new line character. Here is how I'm trying to store the data into the variable.
我在 shell 脚本中有一个变量,我想在其中格式化数据。该变量在循环的每次迭代期间存储新数据。每次存储新数据时,我想插入一个新行字符。这是我尝试将数据存储到变量中的方式。
VARIABLE="$VARIABLE '\n' SomeData"
VARIABLE="$VARIABLE '\n' SomeData"
Unfortunately, the output includes the literal '\n'
Any help would be appreciative.
不幸的是,输出包括文字'\n'
任何帮助将不胜感激。
采纳答案by vmpstr
Try $'\n'
:
尝试$'\n'
:
VAR=a
VAR="$VAR"$'\n'b
echo "$VAR"
gives me
给我
a
b
回答by William Pursell
A common technique is:
一种常见的技术是:
nl='
'
VARIABLE="PreviousData"
VARIABLE="$VARIABLE${nl}SomeData"
echo "$VARIABLE"
PreviousData
SomeData
Also common, to prevent inadvertently having your string start with a newline:
同样常见的是,为了防止无意中让您的字符串以换行符开头:
VARIABLE="$VARIABLE${VARIABLE:+$nl}SomeData"
(The expression ${VARIABLE:+$nl}
will expand to a newline if and only if VARIABLE is set and non-empty.)
(${VARIABLE:+$nl}
当且仅当 VARIABLE 设置且非空时,表达式将扩展为换行符。)
回答by anubhava
Other than $'\n'
you can use printf
also like this:
除了$'\n'
你还printf
可以像这样使用:
VARIABLE="Foo Bar"
VARIABLE=$(printf "${VARIABLE}\nSomeData")
echo "$VARIABLE"
OUTPUT:
输出:
Foo Bar
SomeData
回答by Alex
VAR="one"
VAR="$VAR.\n.two"
echo -e $VAR
Output:
输出:
one.
.two
一。
。二
回答by patrick
I had a problem with all the other solutions: when using a #
followed by SPACE
(quite common when writing in Markdown) both would get split onto a new line.
我对所有其他解决方案都遇到了问题:当使用#
后跟SPACE
(在 Markdown 中编写时很常见)时,两者都会被拆分到一个新行。
So, another way of doing it would involve using single quotes so that the "\n" get rendered.
因此,另一种方法是使用单引号,以便呈现“\n”。
FOO=$'# Markdown Title #\n'
BAR=$'Be *brave* and **bold**.'
FOOBAR="$FOO$BAR"
echo "$FOOBAR"
Output:
输出:
# Markdown Title #
Be *brave* and **bold**.
回答by patrick
Single quote All special characters between these quotes lose their special meaning.
https://www.tutorialspoint.com/unix/unix-quoting-mechanisms.htm
单引号 这些引号之间的所有特殊字符都失去了它们的特殊含义。
https://www.tutorialspoint.com/unix/unix-quoting-mechanisms.htm
So the syntax you use does something different that you want to achieve.
所以你使用的语法做了一些你想要实现的不同的事情。
This is what you need:
这是你需要的:
The $'\X' construct makes the -e option in echo unnecessary.
https://linux.die.net/abs-guide/escapingsection.html
$'\X' 构造使得 echo 中的 -e 选项变得不必要。
https://linux.die.net/abs-guide/escapingsection.html
echo -e "something\nsomething"
or
或者
echo "something"$'\n'"something"
回答by chepner
It's a lot simpler than you think:
它比你想象的要简单得多:
VARIABLE="$VARIABLE
SomeData"
回答by Almendrico
Building upon the first two solutions, I'd do like shown below. Concatenating strings with the '+=' operator, somehow looks clearer to me.
基于前两个解决方案,我希望如下所示。用 '+=' 运算符连接字符串,不知何故对我来说看起来更清晰。
Also rememeber to use printf as opposed to echo, you will save yourself so much trouble
还记得使用 printf 而不是 echo,你会省去很多麻烦
sometext="This is the first line"
sometext+=$'\n\n'
sometext+="This is the second line AFTER the inserted new lines"
printf '%s' "${sometext}"
Outputs:
输出:
This is the first line
This is the third line AFTER the inserted new line
这是第一行
这是插入新行之后的第三行