Linux: Compiling a Bash Script into Binary

PowerADM.com / Linux / Linux: Compiling a Bash Script into Binary

Bash shell scripts in Linux are used in plaintext. Obfuscation allows you to hide a Bash script’s source code by compiling it into a binary. On Linux, you can use the shc tool to compile bash scripts. This tool can convert bash script code into C language and compile it. The output will be a binary file that can be run on almost any other Linux machine.

In this example, we’ll show how to compile a simple Bash script myscript.sh:

#!/bin/bash
echo "Hello, $1?"

Install the shc package (this generic shell script compiler requires gcc):

$ sudo apt install shc gcc

Install Shc (shell script compiler)

To compile your bash script into a binary file, run the command:

$ shc -rf myscript.sh
The -r option allows you to compile a portable binary that can be used on other computers.

The tool will create two files in the current directory:

  • myscript.sh.x – binary file
  • myscript.sh.x.c – source code C file

convert shell script into a binary executable

The binary contents are encrypted and the user won’t be able to view your shell script source code.

binary script file

You can now run the binary:

$ ./myscript.sh.x SuperAdm

Compiling a bash/shell script to binary

With shc, you can specify the program’s validity date. Once it expires, your binary cannot run:

$ shc -e 31/03/2024 -m "Script code has expired!" -rf myscript.sh

./myscript.sh.x: has expired!

shc script has expired

For your convenience, you can rename the resulting binary file:

$ mv myscript.sh.x testapp

Allow the file to run:

$ chmod a+x testapp
To decrypt binary files that were created with an shc version < 4.0.3, you can use the tool https://github.com/yanncam/UnSHc).
Leave a Reply

Your email address will not be published. Required fields are marked *