使用shell脚本将行插入linux中的新文件

时间:2012-03-12 18:30:59

标签: linux shell

我需要编写shell脚本来创建一个新文件并在其中插入一些东西。问题是,使用sed' i / blablabla'命令,它仅在文件中至少存在一行时才有效。插入新文件的命令是什么?

由于

3 个答案:

答案 0 :(得分:2)

echo 'new line' >file_name

此外,您可以使用>>运算符在不使用sed的情况下追加到最后:

echo 'additional line' >>file_name

答案 1 :(得分:1)

更多变种:

cat >>file_being_appended_to <<some_random_string
This is the first line
This is the second line and it has a variable in it; it's right here: $some_variable
some_random_string
cat >>file_being_appended_to <<'some_random_string'
This is the first line
This is the second line and it has a variable reference in it; it's right here: $some_variable,
but this variable reference is not expanded because the beginning delimiter string has single quotes around it.
some_random_string

答案 2 :(得分:0)

其他变体:

echo 'This is the first line
This is the second line.' >file
some_variable="some value"
echo "This is the first line
This is the second line and it has a variable in it; it's right here: $some_variable" >file