I'm trying to create a bash script that parses some multi-line strings, and I also keep a log file for what it's doing.
I use tee -a to output the current step to both stdout and the log file. When I do this, the multi-line string variable changes completely, and I can't for the life of me figure out why this happens.
Can anyone please shed some light on this?
e.g. using this script:
#!/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 "'"
This gives me the following output:
#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
'
why is echo #2 suddenly different?