Skip to content

Bash source obfuscation ​

Requires Codefend 3.3.0 or later for the recommended m_ preset.

Rename a script-local Bash function and its local variables. The script totals three arguments and prints the result without changing shell built-ins.

Use Bash and Node.js/Codefend. On Windows, use a Bash environment such as Git Bash; run the commands from the same project directory.

1. Create the source ​

In a new project directory, install Codefend:

sh
npm install --global codefend

Create source/main.sh:

bash
m_total() {
  local m_sum=0
  local m_value
  for m_value in "$@"; do
    m_sum=$((m_sum + m_value))
  done
  printf '%s\n' "$m_sum"
}

m_total 12 18 12

Expected obfuscated source ​

With the configuration in the next step, Codefend produces:

bash
ox0() {
  local ox1=0
  local ox2
  for ox2 in "$@"; do
    ox1=$((ox1 + ox2))
  done
  printf '%s\n' "$ox1"
}

ox0 12 18 12

The program's expected output remains 42. Generated identifier numbers can change when the source or Codefend version changes.

The m_ names belong to this example. Public output and external API names remain unchanged.

2. Configure selected names ​

Create .codefendrc.json alongside the source directory:

json
{
  "generation": {
    "inputDir": "source",
    "outputDir": "release"
  },
  "transformation": {
    "prefix": "ox"
  },
  "parser": {
    "name": "startWithMAndUnderscore"
  }
}

The startWithMAndUnderscore preset selects this example's m_ names, leaving unrelated underscore names alone. See parser presets for the exact rule and why we recommend this convention.

The lowercase ox prefix produces names such as ox0; the default empty pool avoids a limited replacement dictionary. The plain main.sh filename does not match this rule.

release must be a disposable sibling of source. Codefend removes existing output before generation. Keep this input text-only, with build products and dependencies outside it.

3. Compare original and generated output ​

Run from the project root:

sh
bash source/main.sh
codefend --obfuscate
bash release/main.sh

Both executions should print:

text
42

See Bash shell functions for the language's execution model.

Preserve external names ​

Bash variable and function names are case-sensitive. Preserve exported environment variables, command names, command-line option names, and functions sourced by other scripts. Dynamic names used with eval or indirect expansion need additional checks. This script is for Bash, not every shell.

Codefend matches and replaces text, including text in strings or comments. The preset does not infer language syntax, visibility, or external data contracts. See troubleshooting before applying the convention to a larger project.

Browse the other language and build examples.

Add Codefend to your workflow ​

Add source generation before your script checks or packaging step. Run the generated script through Bash syntax checking, then run your script tests against the generated files.

sh
codefend --obfuscate
bash -n release/main.sh
bash release/main.sh

In CI, stop on a failed step and package only validated generated output. See build and deployment for the shared workflow.