Create a binary file from your bash script
Srijan Kafle
SIEM and SOC Engineer with threat driven mindset | Certified for LogRhythm and LogPoint
Have you ever needed to distribute a Bash script as a standalone executable file? By converting the script to a binary file, you can make it easier to share and execute on different systems without having to worry about specific dependencies or making modifications to the script itself.
In this article, we'll show you how to use the shc tool to convert your Bash script to a binary file. shc is a shell script compiler that encodes and encrypts scripts into executable binaries. The tool converts your script to C source code and then compiles it into a binary executable.
Prerequisites
Before we start, make sure you have shc installed on your system. You can install it using the following command:
sudo apt install shc
Steps to create a binary file
Follow these steps to create a binary file from your Bash script using the shc tool:
- Create a Bash script that you want to compile into a binary file. Let's assume your script is called myscript.sh.
- Open a terminal and navigate to the directory where your script is located.
- Use the following command to compile your script:
shc -rf myscript.sh
- The -r switch is used for redistribution, which allows you to share the script with other users who have the same architecture. The -f switch is used to specify the name of the script to be compiled. This command will create two files in the same directory: myscript.sh.x and myscript.sh.x.c. The former is the binary file that you can execute, while the latter is the C source code that was generated by shc.
- Provide execution permission to the .x file using the following command:
领英推è
chmod +x myscript.sh.x
- This will make the binary file executable.
- You can now execute your binary file using the following command:
./myscript.sh.x
- You can now share the executable file ending with extension x with other users.
Congratulations! You have successfully created a binary file from your Bash script using the shc tool.
Special thanks to Bishesh Shrestha for the inspiration