How to use tee to append to start of file, as opposed to last?

I’m using this bash command to append data to a markdown file:
echo "${{ env.PR_TITLE }}" | tee -a page1.md.

I was wondering if I could append the echo statement to page1.md at the start, so line 1. As opposed to the last line.

If so how can I edit my command above to do so?

Asked By: andres

||

You can’t do it with tee — tee can only overwrite or append.

is appropriate for this job: given file "page1.md":

This is the old first line.
This is the old second line.

Then the command

ed page1.md <<'END_ED'
1i
${{ env.PR_TITLE }}
.
wq
END_ED

results in

$ cat page1.md
${{ env.PR_TITLE }}
This is the old first line.
This is the old second line.

heredoc explained:

  • 1i – insert before line 1
  • ${{ env.PR_TITLE }} – the literal text to insert
  • . – end inserting
  • wq – save and quit

The equivalent

sed -i '1i
${{ env.PR_TITLE }}
' page1.md
Answered By: glenn jackman

Try like

echo "${{ env.PR_TITLE }}" | cat - page1.md > Tmp; mv Tmp page1.md

Check file attributes after the mv.

Answered By: RudiC
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.