0

If I have a file containing newlines, the below script will output the file as is, with newlines:

#!/bin/bash
FOO=$(cat filename.yaml)
echo "$FOO"

but

#!/bin/bash
FOO=$(cat filename.yaml)
FOO=$(echo $FOO)
echo "$FOO"

outputs the file all on one line. How come?

callum
  • 57
  • 6

1 Answers1

0

I do not recommend storing the contents of entire files in a single variable. In my experience that can have unpredictable results.

/usr/bin/env bash -x
index=$(wc -l filename.yaml | cut -d' ' -f1)
count=1

next () {
[[ "${count}" -lt "${index}" ]] && main
[[ "${count}" -eq "${index}" ]] && exit 0
}

main () {
line=$(sed -n "${count}p" filename.yaml)
echo "var${count}=${line}" >> varfile
count=$(($count+1))
next
}

next

If you source varfile at the start of another script, it will give you every line from that file, in its' own variable.

petrus4
  • 616
  • 4
  • 7