Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file with content in one line of bash

Tags:

bash

I wish to create a single file with some contents known to me.

How do I do this in couple lines of bash?

this command will be used inside of a single script, so it should create file, add text, save, and quit automatically by itself without human intervention.

I know that

cat >> some.text
type some stuff
ctrl + D

will work. But is there a pure command line way of doing it?

Thanks

like image 218
Zhen Liu Avatar asked Apr 19 '17 21:04

Zhen Liu


3 Answers

Use a "here document":

cat >> some.text << 'END'
some stuff here
more stuff
END

The delimiter (here END) is an arbitrary word. Quoting this delimiter after the << will ensure that no expansion is performed on the contents.

like image 80
that other guy Avatar answered Sep 30 '22 00:09

that other guy


You could also do the following: echo 'some stuff' > your/file.txt

For multiline, here's another example: printf "some stuff\nmore stuff" >> your/file.txt

like image 36
Jacob Avatar answered Sep 30 '22 00:09

Jacob


For making it multiline its also possilbe to echo in "execution mode":

echo -e "line1\nline2" > /tmp/file

so the \n will make a carriage return.

like image 34
mrks Avatar answered Sep 30 '22 02:09

mrks