I don't want to write the file manually, so I made a shell-script. Is there a way to write & save the file automatically without getting the user to press keys?

sudo nano blah
#write stuff to file
#save file

^ This will be inside a *.sh file

Or is there another way to create a simple text file in a script?

  • 1
    I suspect you are asking the wrong question here. There are much simpler ways to write to a file in a programmatic way than trying to control an editor such as nano. Tell us what you want to do with the file instead. Apr 13, 2014 at 9:35
  • I just want to create it and write a bunch of lines .
    – ntakouris
    Apr 13, 2014 at 9:42
  • Are you asking for a solution something like Autoit for Windows that can automate GUI programs among other things or how to be able to write text form a shell script for example $echo "Some text" > afile.txt?
    – Stef K
    Apr 13, 2014 at 9:53
  • @Zarkopafilis - I've edited your question to make it clearer what you mean. If I've misunderstood you you can always roll it back. I've also posted an answer for you. Apr 13, 2014 at 10:10

2 Answers 2


For more complex sequences of commands you should consider using the cat command with a here document. The basic format is

command > file << END_TEXT
some text here
more text here

There are two subtly different behaviors depending whether the END_TEXT label is quoted or unquoted:

  1. unquoted label: the contents are written after the usual shell expansions

  2. quoted label: the contents of the here document are treated literally, without the usual shell expansions

For example consider the following script


var1="VALUE 1"
var2="VALUE 2"

cat > file1 << EOF1
do some commands on "$var1" 
and/or "$var2"

cat > file2 << "EOF2"
do some commands on "$var1" 
and/or "$var2"

The results are

$ cat file1
do some commands on "VALUE 1" 
and/or "VALUE 2"


$ cat file2
do some commands on "$var1" 
and/or "$var2"

If you are outputting shell commands from your script, you probably want the quoted form.

  • Man how long I searched for this ^^ one question: does this only work in script or is it possible to also use it directly in the shell?
    – derHugo
    Oct 19, 2017 at 5:18
  • @derHugo you can use it directly in the shell - in that case, it would be more usual to omit the END_TEXT and simply use CTRL-D to signal the end of input. See for example How to signal the end of stdin input Oct 19, 2017 at 12:19

There is no need to mess about with an editor to do this.

You can append something to a file with a simple echo command. For example

echo "Hello World" >> txt

Will append "Hello world" to the the file txt. if the file does not exist it will be created.

Or if the file may already exist and you want to overwrite it

echo "Hello World" > txt

For the first line: and

echo "I'm feeling good" >> txt
echo "how are you" >> txt 

For subsequent lines.

At it's simplest the .sh script could just contain a set of echo commands.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .