How to Create TXT Template Scripts in BASH

Bash Doc Templates Featured

If you find yourself creating similar texts in the terminal day in day out, why not simplify the process and save your time by creating a .txt template script for it? If this sounds interesting, fire up a terminal, choose your favorite text editor, and let’s get started!

Create a New Template Script

Create a new bash script whichever way you prefer. We are using the nano editor in terminal to create a file named “” in a “Scripts” folder we have in our home directory:

Bash Doc Templates Create Template Script

Define Your Parameters

Open the file in your favorite editor. At the very top, enter the typical intro that defines it as a bash script:


To keep things clean and legible, enter a commented-out line, with a title for what follows.

The “what follows” part is a bunch of parameters we would like to define in our template. When our template script is complete, we will be able to feed it those parameters to inject them in our text.

We entered “Our parameters” as our title in a commented-out line, as:

#Our parameters

Next, we defined three parameters, “SITE,” “AUTHOR,” and “CONTACT,” by mapping them to three numbered variables:

Bash Doc Templates Define Parameters

You can define different – or more – parameters the same way.

Create Your Template

With our template parameters in place, it is time to create the text template itself.

As before, we start with a commented-out line as a title, as:


Here is what we are adding to the template:

  • We “sandwich” our template between two lines.
  • The first line reads cat << EOF and basically states “everything that follows (AKA: our actual template), until EOF appears, should be treated as a single input.”
  • The second line is the EOF, in this case, “codeword,” that breaks the cat command loop and ends the template.
cat << EOF
Welcome to a Bash-generated TXT template for $SITE.
Created by $AUTHOR.
Contact me at $CONTACT.
Bash Doc Templates Add The Text

A more useful template, though, could come in the form of an email. You could define the “$NAME” and “$TASK” parameters and then craft a template for emails like:

cat << EOF
Hello $NAME,
I just wanted to get back to you regarding $TASK.
Would you be so kind as to update me on any changes?
Best regards,
My Name

After you’re done, save your edits (Ctrl + O) and exit (Ctrl + X) the program.

Next, make your script executable with:

chmod u+x
Bash Doc Templates Make Executable

This way, you will be able to use the name of your script itself to create new documents.

A Test Run

Run your script with the parameters you defined. Our test run looks like this:

./ Make_Tech_Easier Ody
Bash Doc Templates Test Run

The first part is the name of our script, “Make_Tech_Easier” the first parameter, “Ody” the second, and “” the third.

Bash Doc Templates Test Run Results

Note that we used underscores (_) to group the words “make tech easier” together. If you want to use spaces instead, add a quotation mark (for example, “Make Tech Easier”).

Create Document

With your script ready, you can use it as a template to create new documents, thanks to the power of redirects.

To do so, run it like before, as in your test run, but after defining your parameters, enter > path/to/file/filename.txt. In our case, our command looks like:

./ Make_Tech_Easier Ody > /home/ducklord/Documents/new_MTE_note.txt

This way, you instruct your template to output its results to the defined file instead of the terminal. As for the actual results, you can check the following image.

Bash Doc Templates Template Results

That didn’t look very easy!

While it might look complicated, if you follow the instructions above, it is actually very easy. It will also be very useful if you frequently need to create text from the same template. For example, you can automate the creation of regular emails, product information pages, and so on, where the same words or phrases might appear multiple times in the same text.

Or you could go the extra mile, do some additional reading, and use the same approach to create code-generating templates that could, for example, help you set up HTML files containing a whole site’s structure with a single command or to run a bash script as root during startup.

Odysseas Kourafalos
Odysseas Kourafalos

OK's real life started at around 10, when he got his first computer - a Commodore 128. Since then, he's been melting keycaps by typing 24/7, trying to spread The Word Of Tech to anyone interested enough to listen. Or, rather, read.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox