Introduction to Bash Script to Convert Text to PDF in Linux

Converting text files to PDF format is a common task in Linux systems. By creating a Bash script, you can automate this process, saving time and effort. In this tutorial, we will walk through the steps to create a Bash script that converts text files to PDF using command-line tools.


Before we begin, ensure that you have the following prerequisites:

  • Access to a Linux terminal
  • Basic knowledge of Bash scripting
  • Installed command-line tools such as Bash and Ghostscript

Understanding the Bash Script

To start, open your preferred text editor and create a new file. We will name our script You can use the touch command to create the file:


Next, open the file in your text editor and add the following code:

# Bash script to convert text to PDF
if [ -z "$1" ]; then
  echo "Usage: ./ input_file.txt"
  exit 1
enscript -p - "$input_file" | ps2pdf - "$output_file"
echo "PDF created: $output_file"

Let’s break down the script:

  • The first line (#!/bin/bash) is the shebang, which specifies the interpreter to use (in this case, Bash).
  • We check if an input file is provided as a command-line argument. If not, the script displays usage instructions and exits.
  • We define the input file and generate the output file name by replacing the .txt extension with .pdf.
  • The enscript command converts the text file to PostScript format, which is then piped to the ps2pdf command to create the PDF file.
  • Finally, the script displays a message indicating the PDF file creation.

Using the Bash Script

Once the script is created, make it executable using the chmod command:

chmod +x

Now, you can use the script to convert a text file to PDF by providing the input file as a command-line argument:

./ input_file.txt

Replace input_file.txt with the actual name of your text file. After running the script, a PDF file with the same name will be created in the same directory.

Frequently Asked Questions

Q: Can I customize the output PDF file name?

A: Yes, you can modify the script to allow custom output file names based on your requirements. Simply adjust the naming convention within the script to suit your needs.

Q: What if my text file contains special characters or formatting?

A: The enscript command used in the script supports various text formatting options. You can explore its documentation to handle special cases and formatting requirements.

Additional Tips

To enhance the functionality of the script, consider the following tips:

  • Implement error handling to validate the input file and handle potential errors gracefully.
  • Explore options to adjust the layout, font, and other settings for the generated PDF files.
  • Integrate the script into larger automation workflows for document processing.


In this tutorial, we’ve covered the process of creating a Bash script to convert text files to PDF in Linux. By following these steps, you can streamline the conversion process and efficiently handle bulk conversions. Feel free to customize the script to suit your specific requirements and explore additional options offered by the command-line tools used.