This is an edited/updated version an article I posted years ago on World’s Worst Software.
If you’re on windows, you’ll install cygwin, then make sure cygwin’s bin path is added to your $PATH environment variable.
If you’re on unix, linux, or osx – you’ve already got bash and most unix programs you’ll ever need already installed. OSX users looking for this or that unix program that Apple hasn’t included can check out the Homebrew packaging system.
How to setup Bash on your computer
I have written several useful bash scripts and published them here on the Coder Cowboy website for you to freely download and use. Most computer users do not know what a bash script is and most computers cannot run bash scripts without first installing the bash program itself. This article will help clear up what a bash script is and help you through the steps necessary to get bash scripts running on your computer.
Warning: Be Careful With Bash Scripts
Bash scripts are programs. Most any program you run on your computer can run other programs if it wants to once executed. That means a bash script can be just as malicious as a computer virus or any other malicious program. For example, a bash script could delete files off of your hard drive or compromise your system’s security if it was written with a malicious intent.
It is important that you treat bash scripts just like you would treat any other program you install on your computer. Make sure you are confident the programs you are using are authored by reputable sources and always always always scan programs with a virus scanner before you run them.
Fortunately bash scripts are a readable in any text editor. You can always open a bash script in your favorite text editor such as notepad and read through the script’s contents to figure out what it’s trying to do. If your are not confident in a script’s intent or not confident that you understand code you see in a script then seek out the knowledge you need to gain that confidence. I would recommend the Advanced Bash Scripting Guide written by Mendel Cooper as a good starting place to figure out how a bash script is working.
What Is A Bash Script?
In its simplest form, a bash script is a list of commands. If you’re familiar with MS-DOS batch files then one way to think of bash scripts is to think of them as the unix version of batch files. If you haven’t heard of batch files or even MS-DOS then consider the following example…
Let’s say you have a file that you want to copy to three locations every once in a while. Each time you copy the file you have to right click on the file, select copy, then right click in the place you want to copy the file and select paste. If you’re copying the file to three different places then you’re going to be hunting for folders and doing a lot of clicks to finish that copy job! That could be considerably frustrating if you had to copy those files multiple times per day or week. With an MS-DOS command prompt (or Terminal prompt for OSX/Unix users) you could type out the following commands rather than making all of those clicks:
copy myfile.txt c:\location1\myfile.txt
copy myfile.txt c:\location2\myfile.txt
copy myfile.txt c:\location3\myfile.txt
But typing all of that out isn’t any better than making all of those clicks to do the copy paste version of the job! That’s where batch files (or bash scripts) come in. With a batch file you can type those three commands into a text editor like notepad and save the text file as something like CopyMyFiles.bat. Then you could double click on that saved file and your operating system would read and execute all three of the commands you typed into the batch file for you.
That’s the beauty of bash scripts. Bash scripts are basically simplified programs that help us get some grunt work done in an easier way than doing all of the work over and over manually.
Bash is portable, it works anywhere.
Another great thing about bash scripts is that they’re portable, because bash and a lot of other great open source software is portable. Portable software is software that can run on multiple systems.
For example, Microsoft Word is sort of portable because you can run a version of Word on your Windows computer and you can also run a version of Word on your OS X computer.
Bash and the programs my scripts use are totally portable and can be downloaded and installed on your computer for free no matter if you’re running Windows, OS X, or some flavor of unix. Portability is a nice feature of bash and other open source software because you can use it to write bash scripts on one computer (like a windows machine) and then still use the bash script on other machines (like a linux machine) without much hassle.
Bash Environment Setup 101
To get Bash and the programs needed for my bash scripts running on your computer we are going to do the following things:
- Install bash & other open source programs needed by my scripts.
- Create a folder for my scripts and adding it to your PATH.
- Download and test run one of my scripts.
Installing Bash & Other Software On Your Computer
Many bash scripts run with just bash, and that’s it. Others, such as my md5tool.sh script require that you install more than just bash. Fortunately these other unix programs are typically easily installed with the same packaging program you used to install bash.
For example, Bash knows how to do echo, cd, and pwd, but md5sum, find, and grep are not a part of bash. Those commands are seperate programs that must be installed along with bash. Most of the programs used by my bash scripts come as a standard part of your operating system or package system’s default installation selections, but you may run one of my bash scripts and see something like the following:
bash: md5sum: command not found
This means your set of unix utilities doesn’t have the given command installed. You can simply google search for the program and figure out how to install it, for example the md5sum program is a program that comes from the gnu-utils package. Cygwin, brew, and other package managers have this package available to easily install.
Installing On Windows
Windows users will need to download and install several packages that come from the cygwin system. The gracious people at cygwin have made a easy to use package installing system for windows users to get cross platform open source software easily installed on their computer. You simply download the setup.exe file, run it, select the packages you want to install and wait for the installer to update your system for you.
The Cygwin User’s guide will help guide you through the process of installing cygwin packages on your computer.
Bash is part of the default packages that are automatically selected when you start the cygwin installation program, but some tools like bc (a command-line calculator) are not, you’ll need to select those packages to install in the Select Packages window. For example, in the default category view the bc tool is under the Math part of the package list. To install a new package later, simply re-run the setup.exe program and select your packages, and go.
Installing On OSX
OSX users will already have most of the stuff needed by my bash scripts installed on their system since OSX is a unix derivative. However, there are occasionally typical unix utilities that you’ll find missing in certain flavors of OS X.
For example the md5sum utility used by my md5tool.sh bash script is part of OS X 10.3 (panther) on my ibook that I have, but I had to install md5sum manually on my OS X 10.4 (tiger) mac mini computer.
Homebrew is super simple to install, great install instructions are included on the hombrew homepage.
Installing from source
If for some reason you ever need to install a program from a source zip file, Ravi Pratap M’s Getting Started With Open Source Development F.A.Q is a great starting point for learning how to install open source programs from source.
Creating A Script Folder And Adding It To Your PATH
Your operating system finds programs to run by looking for them in a list of directories it knows about. It first searches the directory you’re working in or your shortcut is in and if it can’t find the program there then it tries to find the program you’re trying to run in a pre-defined list of directories to look in for programs. That list of directories is called the PATH and you can edit your system’s PATH to add or remove directories to look in for programs.
We are going to create a new directory to store my bash scripts in and then add that directory to the PATH so your operating system can find my scripts and run them from anywhere on the computer.
First create a directory, for this example I will use the following directory I created on my personal computer:
OS X or Unix users will create a directory that’ll look more like the following:
Next we simply add our new directory location to the end of our PATH variable. Remember that you only need to add your directory to the end of the directory list and should not delete other entries in the directory list.
Windows user’s can read Victor Laurie’s Environment Variables in Windows XP article for instructions on altering their PATH variable.
OS X and unix users will put something like the following in their bash .profile and/or .bashrc file depending on their needs.
Matisse Enzer’s Configuring Your OS X Unix Environment article is a good starting point for beginner OS X and unix admins to learn about where to put the PATH additions.
Download And Test Run One Of My Scripts
Installation of my scripts is pretty easy. You simply download the .sh file from the World’s Worst Software website and place the file in the scripts directory (for example: c:\wwsScripts\) that we just created and added to the operating system path.
An easy test to make sure everything is working correctly is to download filesize.sh and place it in your new scripts folder. Then open a command prompt (or terminal if you are an OS X or Unix user), and type the following command followed by the enter key:
If you see something like the following, then you’ve correctly setup your bash environment and are ready to start using bash scripts!
filesize.sh displays the space usage for the file or directory you specify
FILE_OR_DIRECTORY – the file or directory to display space usage for
If you don’t see output like the above and instead see something like the following then you may not have added the new script directory to your PATH correctly, or you may not have saved the filesize.sh file into your new script directory:
bash: filesize.sh: command not found
If instead you see an error like the following in windows then you may not have setup cygwin correctly and probably need to either reinstall cygwin or add something like c:\cygwin\bin to the FRONT of your PATH list:
'bash' is not recognized as an internal or external command, operable program or batch file.