Skip to main content
TopMiniSite

Back to all posts

How to Execute Shell Script From Latex?

Published on
5 min read
How to Execute Shell Script From Latex? image

Best Software Tools to Buy in October 2025

1 Software Tools

Software Tools

BUY & SAVE
$86.57
Software Tools
2 iWork Mastery for Beginners and Seniors: Create Stunning Documents, Smart Spreadsheets, and Memorable Presentations on Mac, iPad, and iPhone — Unlock ... Tool (Software and Applications Tools Guide)

iWork Mastery for Beginners and Seniors: Create Stunning Documents, Smart Spreadsheets, and Memorable Presentations on Mac, iPad, and iPhone — Unlock ... Tool (Software and Applications Tools Guide)

BUY & SAVE
$17.99
iWork Mastery for Beginners and Seniors: Create Stunning Documents, Smart Spreadsheets, and Memorable Presentations on Mac, iPad, and iPhone — Unlock ... Tool (Software and Applications Tools Guide)
3 Small, Sharp Software Tools: Harness the Combinatoric Power of Command-Line Tools and Utilities

Small, Sharp Software Tools: Harness the Combinatoric Power of Command-Line Tools and Utilities

BUY & SAVE
$21.53
Small, Sharp Software Tools: Harness the Combinatoric Power of Command-Line Tools and Utilities
4 Technical Writing for Software Developers: Enhance communication, improve collaboration, and leverage AI tools for software development

Technical Writing for Software Developers: Enhance communication, improve collaboration, and leverage AI tools for software development

BUY & SAVE
$37.49
Technical Writing for Software Developers: Enhance communication, improve collaboration, and leverage AI tools for software development
5 5299899 for Cummins Inline 7 Data Link Adapter Truck Diagnostic Tool 5572620 with Insite 9.0 Software (Tool with 9.0 Software)

5299899 for Cummins Inline 7 Data Link Adapter Truck Diagnostic Tool 5572620 with Insite 9.0 Software (Tool with 9.0 Software)

  • FASTER PROCESSING & ADVANCED ALGORITHMS FOR REAL-TIME DATA ACCESS.
  • FUTURE-PROOF DESIGN: USB, WIFI, AND BLUETOOTH CONNECTIVITY OPTIONS.
  • TRIPLE CAN SUPPORT; COMPLIANT WITH TMC'S RP1210 STANDARD.
BUY & SAVE
$325.00
5299899 for Cummins Inline 7 Data Link Adapter Truck Diagnostic Tool 5572620 with Insite 9.0 Software (Tool with 9.0 Software)
6 Moho Pro 13.5 | The all-in-one animation tool for professionals and digital artists | Software for PC and Mac OS

Moho Pro 13.5 | The all-in-one animation tool for professionals and digital artists | Software for PC and Mac OS

  • EASILY IMPORT AND ANIMATE BITMAP CHARACTERS WITH PSD INTEGRATION.
  • ADVANCED RIGGING SYSTEM WITH SMART BONES FOR DYNAMIC ANIMATIONS.
  • SIMULATE 3D EFFECTS IN 2D WITH MESH CREATION AND MANIPULATION TOOLS.
BUY & SAVE
$99.00
Moho Pro 13.5 | The all-in-one animation tool for professionals and digital artists | Software for PC and Mac OS
7 Pro Tools Perpetual License NEW 1-year software download with updates + support for a year

Pro Tools Perpetual License NEW 1-year software download with updates + support for a year

  • FULL LICENSE WITH 1 YEAR OF UPDATES FOR MAXIMUM VALUE.
  • INDUSTRY-STANDARD PLATFORM FOR PROFESSIONAL MUSIC AND SOUND PRODUCTION.
  • OVER 60 VIRTUAL INSTRUMENTS AND PLUG-INS TO ENHANCE YOUR SOUND.
BUY & SAVE
$599.00
Pro Tools Perpetual License NEW 1-year software download with updates + support for a year
8 Bootable Windows Password Reset Key USB Flash Drive|Unlock windows password software tool|Compatiblity with Windows 7/Vista/8/10 |Password reset recovery for windows PC & Laptop

Bootable Windows Password Reset Key USB Flash Drive|Unlock windows password software tool|Compatiblity with Windows 7/Vista/8/10 |Password reset recovery for windows PC & Laptop

  • SIMPLE 3-CLICK RESET: EFFORTLESSLY ERASE WINDOWS PASSWORDS IN MINUTES!

  • UNIVERSAL COMPATIBILITY: WORKS WITH ALL WINDOWS VERSIONS-NO EXCEPTIONS!

  • PORTABLE & PRACTICAL USB: 16GB STORAGE FOR FILES; PERFECT GIFT OPTION!

BUY & SAVE
$18.88
Bootable Windows Password Reset Key USB Flash Drive|Unlock windows password software tool|Compatiblity with Windows 7/Vista/8/10 |Password reset recovery for windows PC & Laptop
+
ONE MORE?

To execute a shell script from LaTeX, you can utilize the shell-escape option available in most LaTeX compilers. This allows the LaTeX document to execute shell commands during the compilation process. Firstly, ensure that your LaTeX compiler supports and has enabled shell-escape, which may involve adding a flag like --shell-escape or -shell-escape in your compilation command. Within your LaTeX document, you can use the \write18 command to execute shell scripts. For example, \immediate\write18{./yourscript.sh} will execute yourscript.sh as your document compiles. Keep in mind that this can introduce security risks, so only use shell escape with trusted documents and scripts. Additionally, not all LaTeX environments have shell-escape enabled by default due to these potential security concerns, so you might need to specifically enable it where necessary.

What is the purpose of \texttt{shell-escape} in LaTeX?

The purpose of \texttt{shell-escape} in LaTeX is to allow the execution of external programs or scripts during the compilation of a LaTeX document. When you include the -shell-escape (or sometimes --enable-write18) option in the command line while compiling your LaTeX document (for example, with pdflatex or latex), it permits the LaTeX compiler to run commands outside of the TeX environment.

This feature is particularly useful when using packages that require the inclusion of generated content or perform tasks that are best handled by external tools. Examples of such packages include:

  • minted: for syntax highlighting using the Pygments library, which requires calling an external Python script.
  • gmp and gobble: which might involve processing diagrams or graphics with external software.
  • epstopdf: for converting EPS images to PDF during the compilation.

While enabling \texttt{shell-escape} can be powerful, it also introduces security risks, as it allows execution of arbitrary code, which may potentially harm your system or expose it to vulnerabilities, especially if you are compiling untrusted documents. Therefore, it's important to use this option with caution.

How to use \texttt{shellesc} to minimize security risks?

The \texttt{shellesc} package in LaTeX is used to safely execute external shell commands, minimizing the security risks associated with shell command injection. Here are some general guidelines on how to use the \texttt{shellesc} package to minimize security risks:

  1. Install the package: Ensure that the shellesc package is included in your LaTeX document preamble by adding \usepackage{shellesc}.
  2. Use \ShellEscape: The main command provided by shellesc is \ShellEscape, which securely executes shell commands. Always prefer using this over more risky methods like \write18 (when --shell-escape is enabled in your LaTeX invocation). \documentclass{article} \usepackage{shellesc} \begin{document} Hello, World! \ShellEscape{echo Hello from the shell} \end{document}
  3. Validate Input: Ensure that any input used within \ShellEscape commands is validated and sanitized. Avoid passing untrusted input directly to shell commands to prevent command injection vulnerabilities.
  4. Limit Command Scope: Use shell commands that execute in a limited and controlled manner. Avoid using powerful commands that can affect the system, such as those that modify or delete files, unless absolutely necessary and safe.
  5. Avoid Complex Logic: Keep the logic within your shell commands as simple and minimal as possible to reduce the risk of unintended side effects.
  6. Environment Control: Consider using a controlled environment for executing shell commands, restricting the available environment variables and paths to minimize potential points of attack.
  7. Audit and Review: Regularly audit your code, especially sections that involve external command execution, for potential vulnerabilities or unsafe practices.
  8. Testing: Thoroughly test your LaTeX document in a safe environment to ensure that any shell command execution behaves as expected and does not pose security risks.

By following these guidelines and using the shellesc package appropriately, you can help minimize the security risks associated with executing shell commands from LaTeX documents.

What is a preamble in LaTeX?

In LaTeX, the preamble is the portion of the document that comes before the \begin{document} command. It is used to set up and configure your document. This is where you define the document class, import packages, and set up options and commands that will be used throughout the document.

Here are the main components typically found in a LaTeX preamble:

  1. Document Class: The first command in the preamble usually specifies the document class using \documentclass{}, where you choose a class such as article, report, book, etc. You can also specify options for the document class, such as font size and paper size. \documentclass[12pt, a4paper]{article}
  2. Packages: LaTeX packages add extra functionality to your document. You import them using the \usepackage{} command. For example, \usepackage{graphicx} might be used to include images. \usepackage{graphicx} \usepackage{amsmath}
  3. Custom Commands and Settings: You can define new commands, set up formatting options, and make other configuration choices in the preamble. For example, defining a new command might look like this: \newcommand{\R}{\mathbb{R}}
  4. Other Document Settings: You may include adjustments to spacing, margins, or other stylistic elements via specific package options or with manual commands.

An example of a simple LaTeX preamble might look like this:

\documentclass[11pt]{article} \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{graphicx} \usepackage{geometry} \geometry{margin=1in}

% Custom command \newcommand{\N}{\mathbb{N}}

% Title and author \title{Sample Document} \author{John Doe}

\begin{document} \maketitle

% The main content of the document would go here ...

\end{document}

The preamble is critical because it determines how your entire document is formatted and what features are available to you throughout.