Skip to main content
Skip to main content

How to Minify your scripts

In this document, you’ll learn about how to minify your Bash scripts.

Introduction

Minifying is the process of removing unnecessary characters from your scripts to reduce their size. Also it provides a way to bundle multiple scripts into one file.

There are not a lot of tools for minifying Bash scripts. Argsh uses a modified version of obfus which is a fork.

It's quite experimental and may not work for all scripts. We plan to improve it in the future. If you have any ideas or suggestions, feel free to to reach out to us.

Minify with argsh

Argsh provides a wrapper for obfus that you can use in your projects. If you installed argsh as executable, you can use it like this:

argsh minify scripts/*.sh

Template

Often you need to add back certain parts of your script that are removed during minification (like the shebang). You can use a template file to add these parts back.

argsh minify scripts/*.sh --template ./minified.tmpl

The template file should contain the parts that you want to add back. You can use the following placeholders:

  • ${data}: The minified script
  • ${commit_sha}: The commit SHA of the current git repository
  • ${version}: The version (tag) of the current git repository

Example

#!/usr/bin/env bash
# shellcheck disable=SC2178 disable=SC2120 disable=SC1090 disable=SC2046 disable=SC2155
COMMIT_SHA="${commit_sha}"; VERSION="${version}"
${data}

Ignore line

If you need a variable or line unchanged you can write a comment above it.

# obfus ignore variable

Ignore variables globally

You can ignore specific variable names for obfuscation.

argsh minify --ignore-variable var_name
# or short
argsh minify -i var_name -i foo -i bar
Was this section helpful?