问题描述
我
VAR =A B C
因我是$ var
做
P =`回声-e $ P的'\\ n'$ i`
DONE
回声$ P
我想最后的回声打印
A
b
C
请注意,我想变量p包含换行符。我该怎么做呢?
摘要
-
插入
\\ n
P =$ {VAR1} \\ n $ {} VAR2
回声-e$ {P} -
插入源$ C $ C一个新行
P =$ {VAR1}
$ {} VAR2
回声$ {P} -
使用
$的'\\ n'
(只的和的)P =$ {VAR1}$的'\\ n'$ {} VAR2
回声$ {P}
详细信息
1。插入 \\ n
P =$ {VAR1} \\ n $ {} VAR2
回声-e$ {P}
回声-e
间$ P $点两个字符\\ n
作为新行。
VAR =A B C
first_loop =真
因我是$ var
做
P =$ P \\ N $ I#追加
未设置first_loop
DONE
回声-e$ P#使用-e
避免额外的领先换行
VAR =A B C
first_loop = 1
因我是$ var
做
(($ first_loop))及&放大器; #((...))是具体的bash
P =$ I|| #第一 - >组
P =$ P \\ N $ I#后 - >附加
未设置first_loop
DONE
回声-e$ P#使用-e
使用功能
embed_newline()
{
本地P =$ 1
转移
因为我在$ @
做
P =$ P \\ N $ I#追加
DONE
回声-e$ P#使用-e
}VAR =A B C
P = $(embed_newline $ VAR)#不要用双引号$ VAR
回声$ P
2。插入新行源$ C $ C
VAR =A B C
因我是$ var
做
P =$ P
$ I#新的线直接在源$ C $ C
DONE
需要回声$ P#双引号
#但是-e不需要
避免额外的领先换行
VAR =A B C
first_loop = 1
因我是$ var
做
(($ first_loop))及&放大器; #((...))是具体的bash
P =$ I|| #第一 - >组
P =$ P
$ I#后 - >追加
未设置first_loop
DONE
回声$ P#无需-e
使用功能
embed_newline()
{
本地P =$ 1
转移
因为我在$ @
做
P =$ P
$ I#追加
DONE
回声$ P#无需-e
}VAR =A B C
P = $(embed_newline $ VAR)#不要用双引号$ VAR
回声$ P
3。使用 $的'\\ n'
(不便携式)
并的间$ p $点 $的'\\ n'
作为新行。
VAR =A B C
因我是$ var
做
P =$ P$的'\\ n'$ I
DONE
需要回声$ P#双引号
#但是-e不需要
避免额外的领先换行
VAR =A B C
first_loop = 1
因我是$ var
做
(($ first_loop))及&放大器; #((...))是具体的bash
P =$ I|| #第一 - >组
P =$ P$的'\\ n'$ I#后 - >附加
未设置first_loop
DONE
回声$ P#无需-e
使用功能
embed_newline()
{
本地P =$ 1
转移
因为我在$ @
做
P =$ P$的'\\ n'$ I#追加
DONE
回声$ P#无需-e
}VAR =A B C
P = $(embed_newline $ VAR)#不要用双引号$ VAR
回声$ P
输出是一样的所有
A
b
C
,的,,的和。
修改
- 参见提供了许多细节。
- 并的说明如何避免在上面的 循环的片段。
I have
var="a b c"
for i in $var
do
p=`echo -e $p'\n'$i`
done
echo $p
I want last echo to print
a
b
c
Notice that I want the variable p to contain newlines. How do I do that?
Summary
Inserting
\n
p="${var1}\n${var2}" echo -e "${p}"
Inserting a new line in the source code
p="${var1} ${var2}" echo "${p}"
Using
$'\n'
(only bash and zsh)p="${var1}"$'\n'"${var2}" echo "${p}"
Details
1. Inserting \n
p="${var1}\n${var2}"
echo -e "${p}"
echo -e
interprets the two characters "\n"
as a new line.
var="a b c"
first_loop=true
for i in $var
do
p="$p\n$i" # Append
unset first_loop
done
echo -e "$p" # Use -e
Avoid extra leading newline
var="a b c"
first_loop=1
for i in $var
do
(( $first_loop )) && # "((...))" is bash specific
p="$i" || # First -> Set
p="$p\n$i" # After -> Append
unset first_loop
done
echo -e "$p" # Use -e
Using a function
embed_newline()
{
local p="$1"
shift
for i in "$@"
do
p="$p\n$i" # Append
done
echo -e "$p" # Use -e
}
var="a b c"
p=$( embed_newline $var ) # Do not use double quotes "$var"
echo "$p"
2. Inserting a new line in the source code
var="a b c"
for i in $var
do
p="$p
$i" # New line directly in the source code
done
echo "$p" # Double quotes required
# But -e not required
Avoid extra leading newline
var="a b c"
first_loop=1
for i in $var
do
(( $first_loop )) && # "((...))" is bash specific
p="$i" || # First -> Set
p="$p
$i" # After -> Append
unset first_loop
done
echo "$p" # No need -e
Using a function
embed_newline()
{
local p="$1"
shift
for i in "$@"
do
p="$p
$i" # Append
done
echo "$p" # No need -e
}
var="a b c"
p=$( embed_newline $var ) # Do not use double quotes "$var"
echo "$p"
3. Using $'\n'
(less portable)
bash and zsh interprets $'\n'
as a new line.
var="a b c"
for i in $var
do
p="$p"$'\n'"$i"
done
echo "$p" # Double quotes required
# But -e not required
Avoid extra leading newline
var="a b c"
first_loop=1
for i in $var
do
(( $first_loop )) && # "((...))" is bash specific
p="$i" || # First -> Set
p="$p"$'\n'"$i" # After -> Append
unset first_loop
done
echo "$p" # No need -e
Using a function
embed_newline()
{
local p="$1"
shift
for i in "$@"
do
p="$p"$'\n'"$i" # Append
done
echo "$p" # No need -e
}
var="a b c"
p=$( embed_newline $var ) # Do not use double quotes "$var"
echo "$p"
Output is the same for all
a
b
c
Special thanks to contributors of this answer: kevinf, Gordon Davisson, l0b0, Dolda2000 and tripleee.
EDIT
- See also BinaryZebra's answer providing many details.
- Abhijeet Rastogi's answer and Dimitry's answer explain how to avoid the
for
loop in above bash snippets.
这篇关于试图在bash变量嵌入换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!