Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Shell Scripting - Creating a Binary file
Next article icon

Shell Scripting - Creating a Binary file

Last Updated : 07 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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,

 

Next Article
Shell Scripting - Creating a Binary file

S

strive_to_learn
Improve
Article Tags :
  • Technical Scripter
  • Linux-Unix
  • Technical Scripter 2022
  • Shell Script

Similar Reads

    Shell script to convert binary to decimal
    Binary and decimal are two fundamental number systems used in computing. While humans typically interact with decimal numbers (0-9), computers internally store and process data using binary digits (0s and 1s). Converting between these systems is often necessary when working with computer programs or
    5 min read
    How to Create a Shell Script in linux
    Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
    7 min read
    Introduction to Linux Shell and Shell Scripting
    If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
    8 min read
    Creating and Running bash and zsh Scripts
    Creating and running scripts can be a powerful tool for automating tasks and streamlining workflow. Bash and Zsh are two popular shells that can be used to create and run scripts on Linux and macOS systems. Bash, which stands for Bourne-Again Shell, is the default shell on most Linux distributions a
    7 min read
    Linux Shell Script to Backup Files and Directory
    Backing up our important files and directories is crucial to safeguarding our data. In this article, we’ll create a simple shell script that uses the tar utility to create an archive file for backup purposes that we can use whenever we require. The shell script we’ll build allows us to specify which
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences