echo >> style appending,但是文件的开头

Mik*_*maa 7 unix shell

在UNIX shell中有什么好的技巧可以将行追加到文件的开头吗?比如,类似于

   echo "Foobar" >> file.txt
Run Code Online (Sandbox Code Playgroud)

...但是新线路会先于现有线路吗?

ott*_*t-- 10

分两步:

content=$(cat file.txt) # no cat abuse this time
echo -en "foobar\n$content" >file.txt
Run Code Online (Sandbox Code Playgroud)


Wil*_*ell 6

这是非标准的,但是如果你的sed支持-i你可以这样做:

sed -i '1i\
Foobar' file.txt
Run Code Online (Sandbox Code Playgroud)