How To Create A Bash Install Script

Have you ever wanted to create an installer program on a Linux system, but didn’t want all the hassle of an actual install builder? I have. I recently had need of a simple, no frills installation that could:

  1. ask the user a few questions
  2. extract some binary files and
  3. do some stuff with those binary files.

So I’m going to show you how you can create such an installer with very little hassle. I can’t actually claim credit for this method though; I actually got the idea from Sun’s JDK installer for the Linux platform. You download a “.bin” file, change the file mode so that it is executable and then run it. It displays the end user license agreement, gets some feedback and then goes about installing Java for you. Well, if you open that .bin file up in a text editor (say vi) you’ll see that it’s nothing more than a shell script with a binary chunked onto the end. Thus my plans for world domination were born…

Bash Command Line Pro Tips

As someone that has used the Bash shell almost daily for over 15 years, Jason Cannon has accumulated several command line “tricks” that have saved him time and frustration. This guide is a collection of techniques that you can put to use right away to increase your efficiency at the command line. Start using these 10 tactics today to improve your effectiveness.

Click to download this resource for FREE.

The Script

The first part of this process is to create your install script. At the beginning of my script I put any environment variables and setup that I need for the script to run propertly.

For this exercise I chose a zip file as my binary distribution media. The ZIP_FILENAME variable holds the name of the file after extracting it from the binary installer. SCRIPT_LINES holds the number of lines in this script file. Actually it’s the number of lines plus one (more on this later). SUM1 and SUM2 are the two numbers that I got from running /usr/bin/sum on my zip file. We’ll use these numbers to verify the file after extracting it from the archive.

The next line uses the trap statement to execute some instructions in case the file exits. This just lets the script clean up after itself in the event that the user exits the file prematurely (ie using CTRL-C to close).

Now for the unpacking (or extracting) of our binary distribution media (a zip file in this case). Basically we’re using the tail command to do the job. The -n option indicates that our number argument is the number of lines that we want. Of course you see we’re using the $SCRIPT_LINES variable that we defined at the start of the script. We prepend the plus (+) operator in front of that number which tells the tail command that we want the last lines of the file starting at the number indicated. So we’ll get all of the file starting at the line just past our script, which will be the start of our zip file.

This little bit performs a checksum on the unpacked zip file and checks that against the values we set when building the installer.

The last little bit to this script does not really do that much. We’ve unpacked and verified our zip file so now we just unzip it. To finish up the script removes the zip file (we’ve already extracted it’s contents) and exits. Of course at this stage you could do any number of things that you wanted. You could compile some code if you wanted, prompt the user for feedback in order to build a configuration… whatever you need to do (that can be scripted).

Building The Installer

Ok, so we have our install script ready to go and we have a zip file (or binary distribution media). Now what? Building the final installer is actually a trivial matter from here. All we need to do is concatenate the script file (first) and the zip file into our final installer. We could also change the file mode so that it is executable.

Or if you want to get extremely fancy you can create a makefile! With this example a makefile is like hanging a photo on the wall with a jackhammer, but for more complex projects it may actually be warranted.

With this in a file called Makefile all you have to do is type make and it builds the installer for you.


Thank you so much for stopping by and reading. I sincerely hope that I’ve helped someone out there. To conclude here is a download that includes a) the install script b) the zip file I used and c) the Makefile to build the final installer. Enjoy! (3 KB)

Before you go… please feel to leave comments below. Especially if you have any suggestions as to how this method could be improved. Thanks!

  • Using “make” even for trivial projects can be very useful. You can have the Makefile count the lines in the install_script and update the SCRIPT_LINES variable before stuffing it in the bin. That way you won’t accidentally produce a borked bin file just because you forgot to update the SCRIPT_LINES after your last change 🙂

  • Great suggestion, Sander! I actually do just that on another project that I use this method on. Thanks for commenting.

  • What about introducing some dependency checking? IE, you must have Java 1.5.0_b12 minimum installed, or python 2.5.2. I’m thinking more along the lines of the configure scripts you get with many OS projects…

  • Another great suggestion! I actually do this as well in another project. It basically checks which version of PHP is installed, checks whether a certain configuration file exists in a particular location and actually loads and validates the configuration as well.

    The great thing about this approach is that it is really limitless as to what you can do.

  • Pingback: How To Create A Bash Install Script : HowtoMatrix()

  • Pingback: Bash Script Init Style Status Message |

  • Shyam

    Great article and analysis. This general approach could supplement an RPM installation as well.

  • Nice article, but I guess you didn’t know about Makeself 2.1 ( which has been around for year, and automates everything you have done. Makeself was written by an employee of Loki Games. Makeself is normally available in various distro repositories (Ubuntu example: apt-get install makeself).

  • Tapas

    Thanks a lot for sharing this I came across your blog by chance.Have not tried but will try.

  • Thanks, this was very useful. I ended up making just the mistake Sander Marechal pointed out and so will probably do what he suggested, that enlist “make” to save me from myself.

  • Hi, this is very useful. Thank you very much!

    As to your website: I had to read it with Konqueror, because Firefox puts the sidebar with the social networks _over_ the text.

  • Hmm, that’s not good. I’ll have to check it out on Linux Firefox.

  • The download link for your example is broken at the moment

  • Try again. Sorry about that.

  • Dre

    Great guide, just one small thing the following line:

    “cat install_script > install_script.bin”

    Running that would clobber your script with the zip file, you need to append to that file so it should be:

    “cat install_script >> install_script.bin”
    Note the double greater than signs.

  • Panos

    Thanks a lot Jonathan!
    I copied your example script, added a few ideas of my own (like creating a temp folder with a random name, in order to avoid collisions with existing folders) and created an installer for an application of my own 🙂