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
To compile your bash script into a binary file, run the command:
$ shc -rf myscript.sh
The tool will create two files in the current directory:
- myscript.sh.x – binary file
- myscript.sh.x.c – source code C file
The binary contents are encrypted and the user won’t be able to view your shell script source code.
You can now run the binary:
$ ./myscript.sh.x SuperAdm
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!
For your convenience, you can rename the resulting binary file:
$ mv myscript.sh.x testapp
Allow the file to run:
$ chmod a+x testapp