Skip to main content
2 of 4
added 20 characters in body
Pier A
  • 51
  • 1
  • 5

Write variable containing large text with \n to a file with common shell interpreters. How do you do it?

Environment: linux shell or better, ash shell in busybox (example system: Openwrt).

Given a string variable with large text "${a}" constructed in this way:

for index in $(seq 1 40000); do #it is 40000 for a reason, it helps if you want to reproduce the error on your system.
   a="${a}""\n""word ${index}"
done

and given the fact that trying to use this variable as argument for common command produce an error, for example:

echo "${a}" #fails
awk -v VAR_A="${a}" '<program>' #fails

(the failures are due: http://www.in-ulm.de/~mascheck/various/argmax/ )

How do you write such a variable to a text file, possibly using only simple commands and not sed/awk/perl/other powerful interpreter.

Ideally the text file should look like:

word 1
word 2
word 3
...

and not like:

word 1\nword 2\nword 3\n...
Pier A
  • 51
  • 1
  • 5