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 “MTE_template.sh” in a “Scripts” folder we have in our home directory:

nano MTE_template.sh
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:

#!/bin/sh

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:

SITE=$1
AUTHOR=$2
CONTACT=$3
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:

#Template

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.
EOF
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
EOF

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

Next, make your script executable with:

chmod u+x MTE_template.sh
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:

./MTE_template.sh Make_Tech_Easier Ody mymail@mailserver.com
Bash Doc Templates Test Run

The first part is the name of our script, “Make_Tech_Easier” the first parameter, “Ody” the second, and “mymail@mailserver.com” 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:

./MTE_template.sh Make_Tech_Easier Ody mymail@mailserver.com > /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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox