Compiling Shell Scripts to Executable Binaries with SHC

This article explains how to use the SHC tool to compile shell scripts into executable binaries, covering installation instructions, compilation principles, and usage examples. The tool protects scripts by generating C source code and supports expiration features.

This article was published 1988 days ago, some content may be outdated. If you have any questions, please leave a comment.

Introduction

Official repository address: https://github.com/neurobin/shc

A generic shell script compiler. Shc takes a script specified in the command line and generates C source code. The generated source code is then compiled and linked to produce a stripped binary executable file.

The compiled binary still depends on the shell specified in the shebang line (i.e., #!/bin/sh) of the original shell script. Therefore, shc does not create fully independent binaries.

Shc itself is not a compiler like cc, but rather encodes and encrypts shell scripts while generating C source code with additional expiration functionality. It then uses the system compiler to build a stripped binary that behaves exactly like the original script. When executed, the compiled binary decrypts and runs the code using the shell -c option.

Installation

1
2
3
yum install epel-release
yum -y install gcc gcc-c++ libstdc++-devel
yum -y install shc
1
2
3
4
[root@template mnt]# shc -v
shc parse(-f): No source file specified

shc Usage: shc [-e date] [-m addr] [-i iopt] [-x cmnd] [-l lopt] [-o outfile] [-rvDSUHCABh] -f script

Testing

1
2
shc -v -rf HelloWorld.sh
./HelloWorld.x
Facing the sea with spring blossoms.
Built with Hugo
Theme Stack designed by Jimmy