# adding a line to a file

**URL:** <https://community.unix.com/t/adding-a-line-to-a-file/170077>\
**Category:** Shell Programming and Scripting\
**Created:** [March 26, 2007, 7:06am UTC](https://community.unix.com/t/adding-a-line-to-a-file/170077 "2007-03-26T07:06:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy2000](https://community.unix.com/letter_avatar/andy2000/32/6_22734026db12803ebb3e059622ac3534.png) [@andy2000](https://community.unix.com/u/andy2000)\
**Post date:** [March 26, 2007, 7:06am UTC](https://community.unix.com/t/adding-a-line-to-a-file/170077/1 "2007-03-26T07:06:52Z")

</div>

I want to add a line at the beginning and at the end of a file..

e.g.

echo "at the beginning.." \> tmp\_file &&  
cat file \>\> tmp\_file &&  
echo "last line" \>\> tmp\_file &&  
mv tmp\_file file

is there a nice way for doing that??

Thx

---

<div class="post-metadata">

**Author:** ![dennis.jacob](https://community.unix.com/user_avatar/community.unix.com/dennis.jacob/32/798_2.png) [@dennis.jacob](https://community.unix.com/u/dennis.jacob)\
**Post date:** [March 26, 2007, 7:18am UTC](https://community.unix.com/t/adding-a-line-to-a-file/170077/2 "2007-03-26T07:18:29Z")

</div>

What about this..?

```nohighlight
echo -e "Before the data\n $(cat file)\nEnd of data"

```

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/6_22734026db12803ebb3e059622ac3534.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [March 26, 2007, 8:44am UTC](https://community.unix.com/t/adding-a-line-to-a-file/170077/3 "2007-03-26T08:44:12Z")

</div>

to add a line at beginning of file

```nohighlight
sed '1 i this is first line' file

```

to add at end of file

```nohighlight
sed '$ a this is last line' file

```
