<p>This is probably <a href="http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/x13320.html" rel="noreferrer">a complex solution</a>.</p>

<p>I am looking for a simple operator like ">>", but for prepending.</p>

<p>I am afraid it does not exist. I'll have to do something like </p>

<pre>
 mv myfile tmp
 cat myheader tmp > myfile
</pre>

<p>Anything smarter?</p>

[{'answer_id': 54381, 'author': 'Jason Navarrete', 'author_id': 3920, 'author_profile': 'https://Stackoverflow.com/users/3920', 'pm_score': 7, 'selected': False, 'text': '<p>This still uses a temp file, but at least it is on one line:</p>\n\n<pre><code>echo "text" | cat - yourfile &gt; /tmp/out &amp;&amp; mv /tmp/out yourfile\n</code></pre>\n\n<p>Credit: <a href="http://www.cyberciti.biz/faq/bash-prepend-text-lines-to-file/" rel="noreferrer">BASH: Prepend A Text / Lines To a File</a></p>\n'}, {'answer_id': 54384, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 4, 'selected': False, 'text': '<p>Not possible without a temp file, but here goes a oneliner</p>\n\n<pre><code>{ echo foo; cat oldfile; } &gt; newfile &amp;&amp; mv newfile oldfile\n</code></pre>\n\n<p>You can use other tools such as ed or perl to do it without temp files.</p>\n'}, {'answer_id': 55826, 'author': 'Tim Kennedy', 'author_id': 5699, 'author_profile': 'https://Stackoverflow.com/users/5699', 'pm_score': 1, 'selected': False, 'text': "<p>If you're scripting in BASH, actually, you can just issue:</p>\n\n<pre>\ncat - yourfile  /tmp/out && mv /tmp/out yourfile\n</pre>\n\n<p>That's actually in the Complex Example you yourself posted in your own question.</p>\n"}, {'answer_id': 59504, 'author': 'Eric Hansander', 'author_id': 5039, 'author_profile': 'https://Stackoverflow.com/users/5039', 'pm_score': 4, 'selected': False, 'text': '<p>It may be worth noting that it often is a <a href="http://www.linuxsecurity.com/content/view/115462/81/" rel="noreferrer" title="Safely Creating Temporary Files in Shell Scripts, by Stefan Nordhausen at LinuxSecurity">good idea</a> to safely generate the temporary file using a utility like <a href="http://www.mktemp.org/" rel="noreferrer" title="mktemp homepage">mktemp</a>, at least if the script will ever be executed with root privileges. You could for example do the following (again in bash):</p>\n\n<pre><code>(tmpfile=`mktemp` &amp;&amp; { echo "prepended text" | cat - yourfile &gt; $tmpfile &amp;&amp; mv $tmpfile yourfile; } )\n</code></pre>\n'}, {'answer_id': 61713, 'author': 'dbr', 'author_id': 745, 'author_profile': 'https://Stackoverflow.com/users/745', 'pm_score': 3, 'selected': False, 'text': '<p>When you start trying to do things that become difficult in shell-script, I would strongly suggest looking into rewriting the script in a "proper" scripting language (Python/Perl/Ruby/etc)</p>\n\n<p>As for prepending a line to a file, it\'s not possible to do this via piping, as when you do anything like <code>cat blah.txt | grep something &gt; blah.txt</code>, it inadvertently blanks the file. There is a small utility command called <code>sponge</code> you can install (you do <code>cat blah.txt | grep something | sponge  blah.txt</code> and it buffers the contents of the file, then writes it to the file). It is similar to a temp file but you dont have to do that explicitly. but I would say that\'s a "worse" requirement than, say, Perl.</p>\n\n<p>There may be a way to do it via awk, or similar, but if you have to use shell-script, I think a temp file is by far the easiest(/only?) way..</p>\n'}, {'answer_id': 621786, 'author': 'John Mee', 'author_id': 75033, 'author_profile': 'https://Stackoverflow.com/users/75033', 'pm_score': 6, 'selected': True, 'text': '<p>The <strong>hack</strong> below was a quick off-the-cuff answer which worked and received lots of upvotes. Then, as the question became more popular and more time passed, people started reporting that it sorta worked but weird things could happen, or it just didn\'t work at all. Such fun.</p>\n<p>I recommend <a href="https://stackoverflow.com/a/15721194/75033">the \'sponge\' solution posted by user222</a> as Sponge is part of \'moreutils\' and probably on your system by default.\n<code>(echo \'foo\' &amp;&amp; cat yourfile) | sponge yourfile</code></p>\n<p>The solution below exploits the exact implementation of file descriptors on your system and, because implementation varies significantly between nixes, it\'s success is entirely system dependent, definitively non-portable, and should not be relied upon for anything even vaguely important.  Sponge uses the /tmp filesystem but condenses the task to a single command.</p>\n<p>Now, with all that out of the way the original answer was:</p>\n<hr>\n<p>Creating another file descriptor for the file (<code>exec 3&lt;&gt; yourfile</code>) thence writing to that (<code>&gt;&amp;3</code>) seems to overcome the read/write on same file dilemma. Works for me on 600K files with awk. However trying the same trick using \'cat\' fails.<br></p>\n<p>Passing the prependage as a variable to awk (<code>-v TEXT=&quot;$text&quot;</code>) overcomes the literal quotes problem which prevents doing this trick with \'sed\'.</p>\n<pre><code>#!/bin/bash\ntext=&quot;Hello world\nWhat\'s up?&quot;\n\nexec 3&lt;&gt; yourfile &amp;&amp; awk -v TEXT=&quot;$text&quot; \'BEGIN {print TEXT}{print}\' yourfile &gt;&amp;3\n</code></pre>\n'}, {'answer_id': 1137872, 'author': 'cb0', 'author_id': 85737, 'author_profile': 'https://Stackoverflow.com/users/85737', 'pm_score': 3, 'selected': False, 'text': "<p>assuming that the file you want to edit is my.txt</p>\n\n<pre><code>$cat my.txt    \nthis is the regular file\n</code></pre>\n\n<p>And the file you want to prepend is header</p>\n\n<pre><code>$ cat header\nthis is the header\n</code></pre>\n\n<p>Be sure to have a final blank line in the header file.<br>\nNow you can prepend it with</p>\n\n<pre><code>$cat header &lt;(cat my.txt) &gt; my.txt\n</code></pre>\n\n<p>You end up with</p>\n\n<pre><code>$ cat my.txt\nthis is the header\nthis is the regular file\n</code></pre>\n\n<p>As far as I know this only works in 'bash'. </p>\n"}, {'answer_id': 2362886, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>A variant on cb0\'s solution for "no temp file" to prepend fixed text:</p>\n\n<pre><code>echo "text to prepend" | cat - file_to_be_modified | ( cat &gt; file_to_be_modified ) \n</code></pre>\n\n<p>Again this relies on sub-shell execution - the (..) - to avoid the cat refusing to have the same file for input and output.</p>\n\n<p>Note: Liked this solution. However, in my Mac the original file is lost (thought it shouldn\'t but it does). That could be fixed by writing your solution as: \necho "text to prepend" | cat - file_to_be_modified | cat > tmp_file; mv tmp_file file_to_be_modified</p>\n'}, {'answer_id': 2652178, 'author': 'user318396', 'author_id': 318396, 'author_profile': 'https://Stackoverflow.com/users/318396', 'pm_score': -1, 'selected': False, 'text': '<p>Bah! No one cared to mention about <em>tac</em>.</p>\n\n<pre><code>endor@grid ~ $ tac --help\nUsage: tac [OPTION]... [FILE]...\nWrite each FILE to standard output, last line first.\nWith no FILE, or when FILE is -, read standard input.\n\nMandatory arguments to long options are mandatory for short options too.\n  -b, --before             attach the separator before instead of after\n  -r, --regex              interpret the separator as a regular expression\n  -s, --separator=STRING   use STRING as the separator instead of newline\n      --help     display this help and exit\n      --version  output version information and exit\n\nReport tac bugs to bug-coreutils@gnu.org\nGNU coreutils home page: &lt;http://www.gnu.org/software/coreutils/&gt;\nGeneral help using GNU software: &lt;http://www.gnu.org/gethelp/&gt;\nReport tac translation bugs to &lt;http://translationproject.org/team/&gt;\n</code></pre>\n'}, {'answer_id': 2731521, 'author': 'vinyll', 'author_id': 328117, 'author_profile': 'https://Stackoverflow.com/users/328117', 'pm_score': 0, 'selected': False, 'text': '<pre><code>current=`cat my_file` &amp;&amp; echo \'my_string\' &gt; my_file &amp;&amp; echo $current &gt;&gt; my_file\n</code></pre>\n\n<p>where "my_file" is the file to prepend "my_string" to.</p>\n'}, {'answer_id': 2754039, 'author': 'anonymous', 'author_id': 330877, 'author_profile': 'https://Stackoverflow.com/users/330877', 'pm_score': 4, 'selected': False, 'text': "<p>John Mee: your method is not guaranteed to work, and will probably fail if you prepend more than 4096 byte of stuff (at least that's what happens with gnu awk, but I suppose other implementations will have similar constraints). Not only will it fail in that case, but it will enter an endless loop where it will read its own output, thereby making the file grow until all the available space is filled.</p>\n\n<p>Try it for yourself:</p>\n\n<pre><code>exec 3&lt;&gt;myfile &amp;&amp; awk 'BEGIN{for(i=1;i&lt;=1100;i++)print i}{print}' myfile &gt;&amp;3\n</code></pre>\n\n<p>(warning: kill it after a while or it will fill the filesystem)</p>\n\n<p>Moreover, it's very dangerous to edit files that way, and it's very bad advice, as if something happens while the file is being edited (crash, disk full) you're almost guaranteed to be left with the file in an inconsistent state.</p>\n"}, {'answer_id': 3272296, 'author': 'fluffle', 'author_id': 394737, 'author_profile': 'https://Stackoverflow.com/users/394737', 'pm_score': 5, 'selected': False, 'text': '<pre><code>echo \'0a\nyour text here\n.\nw\' | ed some_file\n</code></pre>\n\n<p>ed is the Standard Editor! <a href="http://www.gnu.org/fun/jokes/ed.msg.html" rel="noreferrer">http://www.gnu.org/fun/jokes/ed.msg.html</a></p>\n'}, {'answer_id': 3835300, 'author': 'shixilun', 'author_id': 463357, 'author_profile': 'https://Stackoverflow.com/users/463357', 'pm_score': 3, 'selected': False, 'text': '<pre><code>sed -i -e "1s/^/new first line\\n/" old_file.txt\n</code></pre>\n'}, {'answer_id': 4027421, 'author': 'Daniel', 'author_id': 243238, 'author_profile': 'https://Stackoverflow.com/users/243238', 'pm_score': 3, 'selected': False, 'text': '<p><strong>EDIT: This is broken. See <a href="https://stackoverflow.com/questions/25334938/weird-behavior-when-prepending-to-a-file-with-cat-and-tee">Weird behavior when prepending to a file with cat and tee</a></strong></p>\n\n<p>The workaround to the overwrite problem is using <code>tee</code>:</p>\n\n<pre><code>cat header main | tee main &gt; /dev/null\n</code></pre>\n'}, {'answer_id': 5209427, 'author': 'hobs', 'author_id': 623735, 'author_profile': 'https://Stackoverflow.com/users/623735', 'pm_score': 2, 'selected': False, 'text': '<p>WARNING: this needs a bit more work to meet the OP\'s needs.</p>\n\n<p>There should be a way to make the sed approach by @shixilun work despite his misgivings. There must be a bash command to escape whitespace when reading a file into a sed substitute string (e.g. replace newline characters with \'\\n\'. Shell commands <code>vis</code> and <code>cat</code> can deal with nonprintable characters, but not whitespace, so this won\'t solve the OP\'s problem:</p>\n\n<pre><code>sed -i -e "1s/^/$(cat file_with_header.txt)/" file_to_be_prepended.txt\n</code></pre>\n\n<p>fails due to the raw newlines in the substitute script, which need to be prepended with a line continuation character () and perhaps followed by an &amp;, to keep the shell and sed happy, like <a href="https://stackoverflow.com/a/723164/623735">this SO answer</a></p>\n\n<p><code>sed</code> has a size limit of 40K for non-global search-replace commands (no trailing /g after the pattern) so would likely avoid the scary buffer overrun problems of awk that anonymous warned of. </p>\n'}, {'answer_id': 5593088, 'author': 'torf', 'author_id': 698365, 'author_profile': 'https://Stackoverflow.com/users/698365', 'pm_score': 2, 'selected': False, 'text': '<p>Why not simply use the ed command (as already suggested by fluffle here)?</p>\n\n<p>ed reads the whole file into memory and automatically performs an in-place file edit!</p>\n\n<p>So, if your file is not that huge ...</p>\n\n<pre><code># cf. "Editing files with the ed text editor from scripts.",\n# http://wiki.bash-hackers.org/doku.php?id=howto:edit-ed\n\nprepend() {\n   printf \'%s\\n\' H 1i "${1}" . wq | ed -s "${2}"\n}\n\necho \'Hello, world!\' &gt; myfile\nprepend \'line to prepend\' myfile\n</code></pre>\n\n<p>Yet another workaround would be using open file handles as suggested by Jürgen Hötzel in <a href="https://stackoverflow.com/questions/2585438/redirect-output-from-sed-s-c-d-myfile-to-myfile">Redirect output from sed &#39;s/c/d/&#39; myFile to myFile</a></p>\n\n<pre><code>echo cat &gt; manipulate.txt\nexec 3&lt;manipulate.txt\n# Prevent open file from being truncated:\nrm manipulate.txt\nsed \'s/cat/dog/\' &lt;&amp;3 &gt; manipulate.txt\n</code></pre>\n\n<p>All this could be put on a single line, of course.</p>\n'}, {'answer_id': 6108287, 'author': 'nemisj', 'author_id': 219703, 'author_profile': 'https://Stackoverflow.com/users/219703', 'pm_score': 2, 'selected': False, 'text': '<p>The one which I use. This one allows you to specify order, extra chars, etc in the way you like it:</p>\n\n<pre><code>echo -e "TEXTFIRSt\\n$(&lt; header)\\n$(&lt; my.txt)" &gt; my.txt\n</code></pre>\n\n<p>P.S: only it\'s not working if files contains text with backslash, cause it gets interpreted as escape characters</p>\n'}, {'answer_id': 7050051, 'author': 'lkraav', 'author_id': 35946, 'author_profile': 'https://Stackoverflow.com/users/35946', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m liking <a href="https://stackoverflow.com/questions/54365/prepend-to-a-file-one-liner-shell/3272296#3272296">@fluffle\'s <strong>ed</strong> approach</a> the best. After all, any tool\'s command line switches vs scripted editor commands are essentially the same thing here; not seeing a scripted editor solution "cleanliness" being any lesser or whatnot.</p>\n\n<p>Here\'s my one-liner appended to <code>.git/hooks/prepare-commit-msg</code> to prepend an in-repo <code>.gitmessage</code> file to commit messages:</p>\n\n<pre><code>echo -e "1r $PWD/.gitmessage\\n.\\nw" | ed -s "$1"\n</code></pre>\n\n<p>Example <code>.gitmessage</code>:</p>\n\n<pre><code># Commit message formatting samples:\n#       runlevels: boot +consolekit -zfs-fuse\n#\n</code></pre>\n\n<p>I\'m doing <code>1r</code> instead of <code>0r</code>, because that will leave the empty ready-to-write line on top of the file from the original template. Don\'t put an empty line on top of your <code>.gitmessage</code> then, you will end up with two empty lines. <code>-s</code> suppresses diagnostic info output of ed.</p>\n\n<p>In connection with going through this, I <a href="http://shawnbiddle.com/devblog/archive/git-commit-templates-vim-and-you" rel="nofollow noreferrer">discovered</a> that for vim-buffs it is also good to have:</p>\n\n<pre><code>[core]\n        editor = vim -c \':normal gg\'\n</code></pre>\n'}, {'answer_id': 7091228, 'author': 'Jayen', 'author_id': 192798, 'author_profile': 'https://Stackoverflow.com/users/192798', 'pm_score': 0, 'selected': False, 'text': '<p>variables, ftw?</p>\n\n<pre><code>NEWFILE=$(echo deb http://mirror.csesoc.unsw.edu.au/ubuntu/ $(lsb_release -cs) main universe restricted multiverse &amp;&amp; cat /etc/apt/sources.list)\necho "$NEWFILE" | sudo tee /etc/apt/sources.list\n</code></pre>\n'}, {'answer_id': 8208478, 'author': 'Hammad Akhwand', 'author_id': 1057325, 'author_profile': 'https://Stackoverflow.com/users/1057325', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s what I discovered:</p>\n\n<pre><code>echo -e "header \\n$(cat file)" &gt;file\n</code></pre>\n'}, {'answer_id': 9485338, 'author': 'Max Tsepkov', 'author_id': 1035328, 'author_profile': 'https://Stackoverflow.com/users/1035328', 'pm_score': 3, 'selected': False, 'text': "<p>Like Daniel Velkov suggests, use tee.<br>\nTo me, that's simple smart solution:</p>\n\n<pre><code>{ echo foo; cat bar; } | tee bar &gt; /dev/null\n</code></pre>\n"}, {'answer_id': 10471758, 'author': 'weakish', 'author_id': 222893, 'author_profile': 'https://Stackoverflow.com/users/222893', 'pm_score': 2, 'selected': False, 'text': "<pre><code>sed -i -e '1rmyheader' -e '1{h;d}' -e '2{x;G}' myfile\n</code></pre>\n"}, {'answer_id': 14077361, 'author': 'Dave Butler', 'author_id': 693869, 'author_profile': 'https://Stackoverflow.com/users/693869', 'pm_score': 0, 'selected': False, 'text': '<p>I think this is the cleanest variation of ed:</p>\n\n<pre><code>cat myheader | { echo \'0a\'; cat ; echo -e ".\\nw";} | ed myfile\n</code></pre>\n\n<p>as a function:</p>\n\n<pre><code>function prepend() { { echo \'0a\'; cat ; echo -e ".\\nw";} | ed $1; }\n\ncat myheader | prepend myfile\n</code></pre>\n'}, {'answer_id': 15721194, 'author': 'user2227573', 'author_id': 2227573, 'author_profile': 'https://Stackoverflow.com/users/2227573', 'pm_score': 4, 'selected': False, 'text': '<p>If you need this on computers you control, install the package "moreutils" and use "sponge". Then you can do:</p>\n\n<pre><code>cat header myfile | sponge myfile\n</code></pre>\n'}, {'answer_id': 17532487, 'author': 'benjwadams', 'author_id': 1914300, 'author_profile': 'https://Stackoverflow.com/users/1914300', 'pm_score': 2, 'selected': False, 'text': "<p>Mostly for fun/shell golf, but</p>\n\n<pre><code>ex -c '0r myheader|x' myfile\n</code></pre>\n\n<p>will do the trick, and there are no pipelines or redirections.  Of course, vi/ex isn't really for noninteractive use, so vi will flash up briefly.</p>\n"}, {'answer_id': 20931105, 'author': 'JuSchu', 'author_id': 1738535, 'author_profile': 'https://Stackoverflow.com/users/1738535', 'pm_score': 2, 'selected': False, 'text': '<p>With <strong>$( command )</strong> you can write the output of a command into a variable.\nSo I did it in three commands in one line and no temp file.</p>\n\n<pre><code>originalContent=$(cat targetfile) &amp;&amp; echo "text to prepend" &gt; targetfile &amp;&amp; echo "$originalContent" &gt;&gt; targetfile\n</code></pre>\n'}, {'answer_id': 25340907, 'author': 'jaybee', 'author_id': 837676, 'author_profile': 'https://Stackoverflow.com/users/837676', 'pm_score': 0, 'selected': False, 'text': '<p>IMHO there is no shell solution (and will never be one) that would work consistently and reliably whatever the sizes of the two files <code>myheader</code> and <code>myfile</code>. The reason is that if you want to do that without recurring to a temporary file (and without letting the shell recur silently to a temporary file, e.g. through constructs like <code>exec 3&lt;&gt;myfile</code>, piping to <code>tee</code>, etc.</p>\n\n<p>The "real" solution you are looking for needs to fiddle with the filesystem, and so it\'s not available in userspace and would be platform-dependent: you\'re asking to modify the filesystem pointer in use by <code>myfile</code> to the current value of the filesystem pointer for <code>myheader</code> and replace in the filesystem the <code>EOF</code> of <code>myheader</code> with a chained link to the current filesystem address pointed by <code>myfile</code>. This is not trivial and obviously can not be done by a non-superuser, and probably not by the superuser either... Play with inodes, etc.</p>\n\n<p>You can more or less fake this using loop devices, though. See for instance <a href="https://stackoverflow.com/questions/5893531/fast-concatenate-multiple-files-on-linux">this SO thread</a>.</p>\n'}, {'answer_id': 26330293, 'author': 'crizCraig', 'author_id': 134077, 'author_profile': 'https://Stackoverflow.com/users/134077', 'pm_score': 1, 'selected': False, 'text': '<p>If you have a large file (few hundred kilobytes in my case) and access to python, this is much quicker than <code>cat</code> pipe solutions:</p>\n\n<p><code>python -c \'f = "filename"; t = open(f).read(); open(f, "w").write("text to prepend " + t)\'</code></p>\n'}, {'answer_id': 28504431, 'author': 'Eric Woodruff', 'author_id': 1139784, 'author_profile': 'https://Stackoverflow.com/users/1139784', 'pm_score': 4, 'selected': False, 'text': '<p>Using a bash heredoc you can avoid the need for a tmp file:</p>\n\n<pre><code>cat &lt;&lt;-EOF &gt; myfile\n  $(echo this is prepended)\n  $(cat myfile)\nEOF\n</code></pre>\n\n<p>This works because $(cat myfile) is evaluated when the bash script is evaluated, before the cat with redirect is executed.</p>\n'}, {'answer_id': 30663695, 'author': 'jozxyqk', 'author_id': 1888983, 'author_profile': 'https://Stackoverflow.com/users/1888983', 'pm_score': 0, 'selected': False, 'text': '<p>Quick and dirty, buffer everything in memory with python:</p>\n\n<pre><code>$ echo two &gt; file\n$ echo one | python -c "import sys; f=open(sys.argv[1]).read(); open(sys.argv[1],\'w\').write(sys.stdin.read()+f)" file\n$ cat file\none\ntwo\n$ # or creating a shortcut...\n$ alias prepend=\'python -c "import sys; f=open(sys.argv[1]).read(); open(sys.argv[1],\\"w\\").write(sys.stdin.read()+f)"\'\n$ echo zero | prepend file\n$ cat file\nzero\none\ntwo\n</code></pre>\n'}, {'answer_id': 31106873, 'author': 'user137369', 'author_id': 1661012, 'author_profile': 'https://Stackoverflow.com/users/1661012', 'pm_score': 1, 'selected': False, 'text': '<p>A solution with <code>printf</code>:</p>\n\n<pre><code>new_line=\'the line you want to add\'\ntarget_file=\'/file you/want to/write to\'\n\nprintf "%s\\n$(cat ${target_file})" "${new_line}" &gt; "${target_file}"\n</code></pre>\n\n<p>You could also do:</p>\n\n<pre><code>printf "${new_line}\\n$(cat ${target_file})" &gt; "${target_file}"\n</code></pre>\n\n<p>But in that case you have to be sure there aren’t any <code>%</code> anywhere, including the contents of target file, as that can be interpreted and screw up your results.</p>\n'}, {'answer_id': 34645273, 'author': 'user5704481', 'author_id': 5704481, 'author_profile': 'https://Stackoverflow.com/users/5704481', 'pm_score': 2, 'selected': False, 'text': "<p>You can use perl command line:</p>\n\n<pre><code>perl -i -0777 -pe 's/^/my_header/' tmp\n</code></pre>\n\n<p>Where -i will create an inline replacement of the file and \n-0777 will slurp the whole file and make ^ match only the beginning.\n-pe will print all the lines</p>\n\n<p>Or if my_header is a file:</p>\n\n<pre><code>perl -i -0777 -pe 's/^/`cat my_header`/e' tmp\n</code></pre>\n\n<p>Where the /e will allow an eval of code in the substitution.</p>\n"}, {'answer_id': 69520112, 'author': '123', 'author_id': 5563327, 'author_profile': 'https://Stackoverflow.com/users/5563327', 'pm_score': 0, 'selected': False, 'text': '<p>for dash / ash:</p>\n<pre class="lang-sh prettyprint-override"><code>echo &quot;hello\\n$(cat myfile)&quot; &gt; myfile\n</code></pre>\n<p>example:</p>\n<pre class="lang-sh prettyprint-override"><code>$ echo &quot;line&quot; &gt; myfile\n$ cat myfile\nline\n$ echo &quot;line1\\n$(cat myfile)&quot; &gt; myfile\n$ cat myfile\nline1\nline\n</code></pre>\n'}, {'answer_id': 70582884, 'author': 'Murilo Perrone', 'author_id': 7626061, 'author_profile': 'https://Stackoverflow.com/users/7626061', 'pm_score': 1, 'selected': False, 'text': "<p>The simplest solution I found is:</p>\n<pre><code>cat myHeader myFile | tee myFile\n</code></pre>\n<p>or</p>\n<pre><code>echo &quot;&lt;line to add&gt;&quot; | cat - myFile | tee myFile\n</code></pre>\n<p>Notes:</p>\n<ul>\n<li>Use <code>echo -n</code> if you want to append just a piece of text (not a full line).</li>\n<li>Add <code>&amp;&gt; /dev/null</code> to the end if you don't want to see the output (the generated file).</li>\n<li>This can be used to append a shebang to the file. Example:\n<pre><code># make it executable (use u+x to allow only current user)\nchmod +x cropImage.ts\n# append the shebang\necho '#''!'/usr/bin/env ts-node | cat - cropImage.ts | tee cropImage.ts &amp;&gt; /dev/null\n# execute it\n./cropImage.ts myImage.png\n</code></pre>\n</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/54365', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1277510/']