ssb
ssb

Reputation: 209

insert line into a new file in linux using shell script

I need to write shell script to create a new file and insert some stuffs into it. The problem is, using sed 'i/blablabla' command, it only works when there is at least one line existed in the file. What is the command to insert into a new file?

Thanks

Upvotes: 0

Views: 5659

Answers (3)

aqn
aqn

Reputation: 2602

More variants:

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

Upvotes: 1

aqn
aqn

Reputation: 2602

Other variants:

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

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295650

echo 'new line' >file_name

Also, you can append to the end without using sed using the >> operator:

echo 'additional line' >>file_name

Upvotes: 2

Related Questions