1

I would like to output results in latex table format using a bash script. However, when I want to add a newline symbol, bash either prints it (\n) or doesn't add a newline. I already tried several things (e.g. the ones mentioned https://superuser.com/questions/154936/echo-text-with-new-line-in-bash), but nothing seems to work. What am I doing wrong?

echo "$(cat $outputFile)$latexString" > $outputFile
echo "\\\\" >> $outputFile
echo "" >> $outputFile

$latexString is the line of the table I want to save, \\ is the EOF for the line in the table, and after that I want to have a newline.

EDIT The output file should look like:

res1 & 1 & 2 & 3 & 4 \\
res2 & 5 & 6 & 7 & 8 \\
...

'latexString' is then e.g.,

res1 & 1 & 2 & 3 & 4
10
  • What do you mean by a "newline symbol"? Do you want to add a literal \n to your LaTeX file? Isn't \\ already the "newline symbol" in LaTeX?
    – Joseph R.
    Commented Nov 7, 2013 at 17:09
  • I want to print several latexStrings on different lines. Each line needs to have \\` at the end of the line. So it cannot print \n`, but needs to put the content on separate lines instead.
    – MaVe
    Commented Nov 7, 2013 at 17:10
  • It might be better if you provided the last line of $outputFile and what $latexString is. There might be better way of approaching this.
    – phemmer
    Commented Nov 7, 2013 at 17:11
  • 2
    By echo "$(cat $outputFile)$latexString" > $outputFile, do you mean echo "$latexString" >> $outputFile?
    – choroba
    Commented Nov 7, 2013 at 17:19
  • 1
    @MaVe This is likely to truncate $outputFile. Why aren't you using >> instead?
    – Joseph R.
    Commented Nov 7, 2013 at 17:20

2 Answers 2

2

The solution is

echo "$latexString\\\\" >> $outputFile
0

printf will do the trick:

printf "%s %s\n" "$latexString" "\\\\" >> $outputFile

or even more readable:

printf "%s %s\n" "$latexString" '\\' >> $outputFile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .