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 January 2026

1 Software Tools

Software Tools

BUY & SAVE
$69.14
Software Tools
2 Avid Pro Tools Artist - Music Production Software - Perpetual License

Avid Pro Tools Artist - Music Production Software - Perpetual License

  • SEAMLESS AUDIO PRODUCTION FROM IDEA TO FINAL MIX WITH PRO TOOLS.

  • TRUST THE INDUSTRY STANDARD, USED BY PROS IN MUSIC AND FILM.

  • MONTHLY UPDATES AND EXCLUSIVE CONTENT TO KEEP CREATIVITY FLOWING!

BUY & SAVE
$199.00
Avid Pro Tools Artist - Music Production Software - Perpetual License
3 Programming with GNU Software: Tools from Cygnus Support (Nutshell Handbooks)

Programming with GNU Software: Tools from Cygnus Support (Nutshell Handbooks)

BUY & SAVE
$18.11 $39.95
Save 55%
Programming with GNU Software: Tools from Cygnus Support (Nutshell Handbooks)
4 Avid Pro Tools Ultimate Perpetual Recording Software (Boxed)

Avid Pro Tools Ultimate Perpetual Recording Software (Boxed)

  • MIX 2,048 AUDIO & 512 INSTRUMENT TRACKS FOR EPIC PROJECTS!
  • RECORD 256 AUDIO SOURCES AT ONCE FOR ULTIMATE VERSATILITY!
  • ACCESS 120+ VIRTUAL INSTRUMENTS AND PLUGINS FOR ENDLESS CREATIVITY!
BUY & SAVE
$1,499.00
Avid Pro Tools Ultimate Perpetual Recording Software (Boxed)
5 The Open Disc 11.09 - All the Essential Software Tools You'll Ever Need

The Open Disc 11.09 - All the Essential Software Tools You'll Ever Need

  • ACCESS 50+ TOP OPEN-SOURCE SOFTWARE FOR FREE!
  • SAVE $1000S COMPARED TO OVERPRICED RETAIL ALTERNATIVES.
  • NO REGISTRATION NEEDED-INSTALL ON MULTIPLE DEVICES HASSLE-FREE!
BUY & SAVE
$5.99
The Open Disc 11.09 - All the Essential Software Tools You'll Ever Need
6 Generative AI for Software Developers: Future-proof your career with AI-powered development and hands-on skills

Generative AI for Software Developers: Future-proof your career with AI-powered development and hands-on skills

BUY & SAVE
$46.74 $54.99
Save 15%
Generative AI for Software Developers: Future-proof your career with AI-powered development and hands-on skills
7 STREBITO Small Precision Screwdriver Set 64-piece with Torx, Triwing, Pentalobe, Electronics Repair Tool Kit for Computer, PC, Laptop, Macbook, Tablet, Phone, PS5, XBOX, Switch, Glass, Ring Doorbell

STREBITO Small Precision Screwdriver Set 64-piece with Torx, Triwing, Pentalobe, Electronics Repair Tool Kit for Computer, PC, Laptop, Macbook, Tablet, Phone, PS5, XBOX, Switch, Glass, Ring Doorbell

  • VERSATILE 64-IN-1 SET: REPAIR ANYTHING FROM GADGETS TO GAME CONSOLES!

  • ERGONOMIC DESIGN: ENJOY COMFORT AND CONTROL WITH A MAGNETIC SWIVEL CAP.

  • COMPACT STORAGE: ORGANIZED CASE FOR EASY TRANSPORT AND QUICK ACCESS.

BUY & SAVE
$9.99
STREBITO Small Precision Screwdriver Set 64-piece with Torx, Triwing, Pentalobe, Electronics Repair Tool Kit for Computer, PC, Laptop, Macbook, Tablet, Phone, PS5, XBOX, Switch, Glass, Ring Doorbell
8 WavePad Audio Editing Software - Professional Audio and Music Editor for Anyone [Download]

WavePad Audio Editing Software - Professional Audio and Music Editor for Anyone [Download]

  • COMPREHENSIVE EDITING TOOLS: RECORD, EDIT, AND ENHANCE AUDIO SEAMLESSLY.

  • DIVERSE FORMATS SUPPORTED: WORK WITH ALL POPULAR AUDIO FORMATS EFFORTLESSLY.

  • VST PLUGIN INTEGRATION: EXPAND CAPABILITIES WITH THOUSANDS OF PROFESSIONAL EFFECTS.

BUY & SAVE
$69.99 $99.00
Save 29%
WavePad Audio Editing Software - Professional Audio and Music Editor for Anyone [Download]
+
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.