Shell Scripting – Creating a Binary file
While working in Linux systems, we have used so many commands on a day-to-day basis. Most of the commands are in the binary format resides under /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, etc directories. As system administrators, we would have to write many shell scripts to do a few tasks or automate them. In this article, we shall see how to create a binary file from our own shell script so that no one can see the source code and it can also be used as a normal system command.
What are Binary files?
A binary file is a type of computer file that is used to store data in the form of bytes. It can be used to store any kind of data either formatted or unformatted data encoded within the binary format. It is used directly by the computer and non-human readable which is also called binaries. The header of the binary file defines the file type and other relevant information like the block and body sizes. The header might include a few human-readable characters, but a binary file as a whole requires specific software or hardware to read the file.
Binary files are commonly used when building applications/software’s. But, programmers do not work directly with the binary files. Instead, they build their applications in a high-level programming language such as C, C++, or Java. The languages use human-readable text files to define the logic. At some point in their development cycle, application logic is compiled and converted into object code which is then called an executable or binaries with extensions such as .bin or .exe.
The below steps would help to create our own binary file from the shell script,
Pre-requisites
To create a binary from the shell script, the user needs to install the SHC (Shell Script Compiler) which can be done in two following ways,
Installation of shc
Method 1: From the repository
It can be downloaded and installed from the repository using the below commands,
For Ubuntu/Debian :
sudo apt install shc
For RHEL/Centos/Fedora:
sudo yum install shc
Method 2: Download and Install SHC
SHC can also be installed by downloading the source RPM from the official webpage using the below commands,
cd /root/shc
wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.9.tgz
tar xzf shc-3.8.9.tgz

Now let’s compile the SHC source code and install it using “make install” which will copy the shc binary to /usr/local/bin directory,
cd shc-3.8.9 make install
Thus shc is successfully installed in the system,
Example of Binary File:
Step 1: Create a shell script
We will write a simple shell script add.sh which takes a number as input from the user and display the sum value.
#!/bin/bash total=0 for i in $@; do if [ ! -z "${i##[0-9]*}" ]; then echo "Please enter numeric only" exit 1 fi total=$(($total + $i)) done if [ $total -eq 0 ]; then echo "Please execute script like: $0 10 20 30" exit 0 fi echo "The sum is $total"
Step 2: Create a Binary
Using SHC, we will create a binary from add.sh using the below command,
shc -f add.sh
The above command creates two files in the current directory where the command is run. One will be add.sh.x.c which is in C language format and the other one is add.sh.x which is the binary file.
Step 3: Test the binary
Thus we have successfully created the binary file from our add.sh script and it’s time to move it to /usr/bin directory for the binary to work like a standard system command. Also, set execute permission for the script.
mv add.sh.x /usr/bin/add chmod +x /usr/bin/add
Now running the binary anywhere in the system would give the same result as the shell script does,