Sie sind auf Seite 1von 14

Learn Bash in Windows

Salvador Garcia DATN20160915

1. Set up Win Bash
2. Create shortcut to open bash
3. Get a nice code editor
4. Create and run scripts

Setup Win-Bash
To do bash programming in Windows we need to get Win-Bash. This is a version of Bash for Windows
that was ported from the GNU Bash 1.14.2. The Website where to get it follows:

Scroll down and look under Further Information and locate the download link. Click that.

Once at the page, click on the Download (5.6 MB) link. Yes, you can also click on the
Download file that in the list just below.

A new page will open following by a three-second delay.

Next, define and if necessary, create a folder where you want to have win-bash and its corresponding

Go into the ZIP and extract all the files to the chosen folder.

Take note of the folder where these files were placed. I placed my files in a folder called C:\win-bash.

Next, we need to create a batch file to set the bash files path and open bash automatically when we
double click on the Command Prompt icon that well be creating shortly.

The text for the batch file is the following:

@echo off
SET PATH=%PATH%;bashfoldername

Nothing complicated. Create this batch file, replace the bashfoldername placeholder with the full path
of the folder where the bash files were placed, name it whatever you want and save it in the same
folder where the batch files were copied. I named my batch file setpath.bat. Make sure that it has the
.BAT extension.

The next image shows what my batch file looked like.

The last step in this process is to create a work folder. It can be anywhere, as long as the user has access
to it. Also, it is recommended that the folder reside outside of the folder where the bash files were
placed. I created my work folder at c:\bash. (In retrospect, I probably should have named it WorkBash.)

Create shortcut to open Bash

We want a nice icon on the desktop that when double clicked will open a Command Prompt with the
bash shell running. Click the Start button, select Accessories and right click the Command Prompt Icon.

Select Send to from the context menu and then select Desktop (create shortcut) from the sub-menu,
This will create a Command Prompt icon on the desktop. Now that the short cut has been create well
modify it a bit to give it a bash personality.

Right click the newly created desktop icon and select Rename from the context menu. Or, instead of
that, click the icons text twice, waiting about a second before the second click.

The icons text will now be highlighted and inside a small text box. Press the Del key to remove the
highlighted text and enter the new name: bash. Press Enter to finish the operation. The Command
Prompt now displays a new name.

The next step is to configure our command prompt desktop icon to run bash whenever it is double
clicked. To do this first open its properties window by right clicking the icon and then selecting
Properties from the context menu.

We will be modifying the Target and Start in fields of this dialog.

The Target field will be %windir%\system32\cmd.exe

Change that to the following:

%windir%\system32\cmd.exe /k "C:\win-bash\setpath.bat"

You will substitute the above text in quotes with the name of the folder where you placed the extracted
bash file and the .bat file name with the filename that you used for the batch program that we created
previously. The quotes are not necessary unless you decided to have a space in the folder or filename.

The Start in field will be %HOMEDRIVE%%HOMEPATH%.

Change that to the following:

This is the name of the work folder that you created earlier. I named mine C:\bash. You must use
whatever name you gave your folder.

That concludes configuring the desktop bash icon. Click the Ok button to finish the operation and
double click the icon.

Lets test it. As said above, double click the bash icon to open the bash window. A window as shown in
the next image should come up. Now enter pwd and press enter. The response should be the display of
the name of the work folder.

To close/exit bash you can use brute force and click on the red X at the top right corner of the window
or you can give the bash command exit. I personally like exiting by entering exit and pressing the Enter
key. Follow your heart.

Get a Nice Code Editor

The scripts that youll be writing will require a code editor. Before anything else, Ill just say it. You can
use Notepad. I did for years and years, until I found something prettier. I added quotes to that word
because although it does look better, it is also helpful in that the editor colorizes words inside the bash
script so that it is easier to distinguish variables, keywords and strings, among other types of text entry.

The new code editor that I use is NotePad++. This application can be used free of charge. The download
page is the following:

Once at the site, click the Download link in the left panel, scroll down a bit, and then click the green
Download button. The installer will download to your computer. Run the installer and install

Depending on the installation selected, the NotePad++ start up icon will be on the Desktop or Start
Menu. I placed mine on the Start Menu, so I start it by clicking the Start button then clicking the
NotePad++ icon.

Your NotePad++ startup process may be different depending on how you decided to install NotePad++.

Next, well configure NotePad++ so that it recognizes our bash scripts and its default folder is our work
folder c:\bash.

After opening NotePad++ select Language from the main menu, then select the letter S and finally
select Shell. This tells Notepad++ that well be using the bash shell as our programming language.

Now to set the default folder.

From the main menu select Settings and then Preferences.

When the dialog displays, select Default Directory in the left panel. Then click the third option button
followed by the ellipsis button. Navigate to the c:\bash folder and select that.

Also, you can directly enter the work folder name into the text field. Note that you can also select the
Remember last used directory. When you save your first script to c:\bash NotePad++ will remember
that folder and use it from then on until you open or save from or to a different folder.

To complete the operation, after youve selected the folder, click the Close button.

Pop quiz!

Which do you prefer? Option (a) or option (b)?

Create and Run Scripts

To test our bash script development platform lets create the obligatory Hello World program and run it
in the win-bash environment.

Open NotePad++ (if not already open) and enter the following script:

echo Hello World

Save the script to the file

Note how the Save dialog automatically went to the c:\bash folder. No need to navigate to it! Enter the file name and click the Save button. Done!

Now lets switch to the bash environment. Double click the bash desktop icon to open the bash session.

Once that is done enter pwd to verify that we are in the c:\bash work folder.

Now enter ls l (thats the letter L, S, space, dash and another L) to list the files in that folder. The that we just created should be in there.

Note: The ls command can be entered in either lower or upper case, but the argument after the dash
needs to be in lower case. Otherwise your display will look different. The bash$ text to the far left is
called the prompt. Bash displays a prompt whenever it is ready for input by the user. Normally, it will
display the current folder followed by the $ for normal users or a # for the root user.

To run the script enter the following after the prompt:

Hello World should display.

The ./ tells bash that the script file is located in the current directory, as given by the pwd command
(print working directory). If these two characters are omitted bash will look only in its own directory
where it is installed. In this case, it wont find it and will generate the following message:

bash: command not found

Another problem that may come up in a real bash environment is that the script will not run because it
does not have the execute permission. This permission, when granted to an executable file, allows that
file to be executed. In the case of the win-bash environment, this permission did not seem to be
necessary, but in other environments (such as Linux), it is absolutely necessary. Note the directory listing
in a previous image. The -rw-rw-rw- string is found to the far left. This sequence of characters presents
the permissions that have been granted to the file. The execute permissions are displayed as an X.
Clearly our script does not have execute permissions.

To grant the file execute permissions you can use the following command:

chmod a+x

However, Windows NT does not support this win-bash command, so I imagine that it is lenient in
allowing scripts that do not have the execute permission to execute. Also note that in the example
above I granted execute permissions to everyone. Change this accordingly.

Windows 10
While this document was being created in late August of 2016, Microsoft released an update to
Windows 10. Among other pleasantries or curses, this update allows users to set up a native bash
environment. The content presented in previous sections was created using Windows 7. This section
presents some of the steps that are slightly different for Windows 10 users that meet one of the
following criteria:

a. Do not want to run the native bash environment in updated Windows 10 installations
b. Do not want to install Microsofts latest update to Windows 10 (although it seems that in most
cases, there isnt any choice)

The setpath.bat file remains the same, as illustrated in the next image.

The most noticeable difference is how the Desktop shortcut is created. Windows 10 follows a slightly
different procedure:

1. Right click anywhere on the Desktop.

2. From the context menu, select New.
3. From the sub-menu select Shortcut.

This will open a mini-wizard where we need to enter the following:

a. The full path specification of the item to be run when the short cut is launched by double
clicking on it or equivalent.
b. The displayed name (or caption) of the shortcut.

The next three images show this process.

The user right-clicked the Desktop and then selected the New/Shortcut options.

The file is cmd.exe and it is located in the system32 of the Windows folder. Since Windows keeps the
path for its own files in the WINDIR environment variable, we just need to say %windir% (case is not
important) and Windows will substitute the correct folder path where its own files are stored. The
complete sting is the following: (Note: The image above does not show the entire string as below)

%windir%\system32\cmd.exe /k c:\win-bash\setpath.bat
When the shortcut is launched the cmd.exe file is executed which opens a Command Prompt window.
Once the Command Prompt is open it automatically executes the setpath.bat file. This file sets up the
PATH environment, launches bash and finally closes the Command Prompt window when the user closes
the bash session with the exit command.

This just provides a name for the shortcut. This name is the caption that appear below the shortcut icon.

The next step is to verify that the shortcuts properties are correctly set and that bash work folder is
specified. To open the properties window of the shortcut do the following:

1. Right click the short cut icon.

2. Select the Properties option from the context menu.

The fields to modify are the Target and Start in fields, as was done in the Windows 7 procedure. Check
the Target field. It may already contain the full string with the /k switch and the setpath.bat file
specification. In case it does not, enter the following after cmd.exe and a space:

/k c:\win-bash\setpath.bat

The Start in folder is our work folder. In my case, I created d:\bashWork for this purpose. Enter the work
folder path specification and finish up the process by clicking the OK button.

Double click the shortcut icon to open a bash session. If it does not work, or the Command Prompt
window opens and closes rapidly, verify the data in the Target and Start in fields.

Also verify that all the folder specs coincide with these two text field in the Properties window.

Once the bash environment opens enter any test command (I find pwd quite rewarding) to verify that all
is in working order.

Make sure that the, or whatever other script you have, is in the d:\bashWork folder. Test this by
using the ls bash command to get a listing of the files in the pwd directory.

And thats all!

Video of this procedure: