为什么在bash脚本中$LINES会改变?

19 浏览
0 Comments

为什么在bash脚本中$LINES会改变?

我正在尝试创建一个解析一些多行字符串的bash脚本,并且我也保留了一个日志文件以记录它的操作。

我使用tee -a命令将当前步骤的输出同时输出到标准输出和日志文件中。

当我这样做时,多行字符串变量完全改变了,我无法理解为什么会发生这种情况。

请问有谁能解释一下这是为什么吗?

例如使用以下脚本:

#!/bin/bash
#
LINES=""
LINES="$(echo -e "$LINES\\n some text 1")"
LINES="$(echo -e "$LINES\\n some text 2")"
LINES="$(echo -e "$LINES\\n some text 3")"
LINES="$(echo -e "$LINES\\n some text 4")"
LINES="$(echo -e "$LINES\\n some text 5")"
echo "#1"
echo "'"
echo "$LINES"
echo "'"
echo -e "\nnow doing thing A" | tee -a "log.txt"
echo "#2"
echo "'"
echo "$LINES"
echo "'"
echo ""
LINES=""
LINES="$(echo -e "$LINES\\n some text 1")"
LINES="$(echo -e "$LINES\\n some text 2")"
LINES="$(echo -e "$LINES\\n some text 3")"
LINES="$(echo -e "$LINES\\n some text 4")"
LINES="$(echo -e "$LINES\\n some text 5")"
echo "#3"
echo "'"
echo "$LINES"
echo "'"
echo -e "\nnow doing thing B"
echo -e "\nnow doing thing B" >> "log.txt"
echo "#4"
echo "'"
echo "$LINES"
echo "'"

这给我以下输出:

#1
'
 some text 1
 some text 2
 some text 3
 some text 4
 some text 5
'
now doing thing A
#2
'
61
'
#3
'
 some text 1
 some text 2
 some text 3
 some text 4
 some text 5
'
now doing thing B
#4
'
 some text 1
 some text 2
 some text 3
 some text 4
 some text 5
'

为什么echo #2突然不同了?

0