I am trying to create a text file foobar.txt
using \immediate\write18
, and can not figure out how to properly get the \n
so that Unix puts these lines on a new line. The MWE below produces one line in the output file:
\n \n ———\n Foo: foo \n Foo: foo \n Bar: bar \n
What I want in the output file is a few blank lines followed by:
———
Foo: foo
Bar: bar
Additional Question:
- This appears to always append to the file, where as I thought the normal Unix way is that single
>
overwrites the file, and the double>>
appends to the file.
How can I get it to overwrite the file if it already exists. Or is this a security feature that prohibits this.
Notes:
- The values after the colon are not fixed, but are built during compile time. Otherwise a simple
cat
solution would have worked.
Code:
\documentclass{article}
\begin{document}
\immediate\write18{%
echo " \string\n" > foobar.txt%
echo " \string\n" >> foobar.txt%
echo "---------\string\n" >> foobar.txt%
echo "Foo: foo \string\n" >> foobar.txt%
echo "Foo: foo \string\n" >> foobar.txt%
echo "Bar: bar \string\n" >> foobar.txt%
}
\end{document}
Best Answer
Just concatenate the
echo
commands:If the
noclobber
variable is set, the shell will refuse to overwrite an existing file with an output redirection command. So if the variable is set in the environment of the called command shell, the firstecho "" > foobar.txt
will not succeed.However the same result will be obtained with the standard stream writing: