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:

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:

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

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

Next, make your script executable with:

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:

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:

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.

Related:

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.