Zero logo

Unix Shebang on Windows

This page covers running Perl scripts with Unix Shebang unmodified on Windows. It allows you to easily transfer scripts between Windows and Unix systems and to run scripts from third parties that use a Unix Shebang.

Note: The implementation is for a fixed installation of Uniform Server running either as a standard program or as a service on a modern version of Windows that supports symbolic links this excludes Windows XP.

Shebang interpreter directive

The syntax of a shebang (interpreter directive) is as follows: #! interpreter [optional-arg]
Note: The #! means "use the following command/interpreter to run this script". Generally the command/interpreter is the absolute path to an executable for example:

Unix format:

Note: The forward slash is shorthand meaning top-level of the current drive. For example to find the perl.exe executable on C drive, start at the top-level of C: drive, look in in folder usr and then folder bin for the perl.exe executable.

Default installation of Uniform Server executable path is:

For a default instalation of Uniform Server on C drive. Start at the top-level of C: drive, navigate the folder tree UniServerZ, core, perl finally look in in folder bin a for the perl.exe executable.

Note: In both cases the .exe extension is optional, generally the Unix Shebang excludes this extension.

Symbolic link

From the above example a Unix Shebang will not find the perl.exe executable on a default installation of Uniform Server. The path /usr/bin/perl does not exist what is required is to map the Unix Shebang to the real path C:/UniServerZ/core/perl/bin/perl.exe this is achieved using a symbolic link.

Path difference
The following shows the two paths one above the other for comparison.

                 C:/usr/bin/perl.exe  - Unix path
C:/UniServerZ/core/perl/bin/perl.exe  - Uniform Server path

Starting from right to left remove common elements to give:

                 C:/usr  - Unix path
C:/UniServerZ/core/perl  - Uniform Server path

Link redirection
When searching for perl.exe, Unix Shebang directs the search to folder usr. Instead of folders in usr it finds a link and follows this. At the end of the link redirection path it appends folder bin and looks for perl.exe The link redirection is referred to as a symbolic link, it contains a real path for example "C:/UniServerZ/core/perl"

mklink syntax
A symbolic link is created with the Microsoft DOS command mklink. It has the following syntax:

mklink /d Link Target
Parameter Description
/d Creates a directory symbolic link. Default is a file symbolic link.
Link Specifies the name of the new symbolic link that is being created.
Target Specifies the path (relative or absolute) that the new symbolic link refers to.

Note 1: The mklink command is an internal command that is available in the following operating systems:

  • Windows 7
  • Windows Vista
  • Windows 8
  • Windows 10

Note 2: The mklink command requires an elevated command prompt.

Creating a symbolic link to access the Perl executable

The above looks complex in reality is is quite simple, all that is required is to map folder usr to folder path C:/UniServerZ/core/perl. using the following command line:

mklink /D usr C:\UniServerZ\core\perl
Server relocation

Uniform Server is portable and can be moved to a different location either to a different folder or hard drive by copying folder UniServerZ and all its content to the desired location. However you must remember to adjust the path accordingly in the symbolic link. This inconvenience is avoided by using two batch files.

Batch files

The following two batch files allow you to easily create and delete a symbolic link to access the Perl executable.
Create and test these batch files as follow:

Create two new files link_create.bat and link_delete.bat in folder UniServerZ with the following content:

@echo off
rem working directory current folder
pushd %~dp0

rem save us path
set us_path=%cd%

rem change to top level
cd \

rem create link
mklink /D usr %us_path%\core\perl

rem restore original
@echo off
rem working directory current folder
pushd %~dp0

rem save us path
set us_path=%cd%

rem change to top level
cd \

rem delete link
rd usr

rem restore original
rem pause
  • pushd %~dp0: Saves return address and current location of the batch file.
  • us_path: The variable us_path is set to the current working directory. Full path of batch file location.
  • cd \: Change working directory to top-level of disk drive.
  • mklink /D usr %us_path%\core\perl:
    • usr: A link folder usr is created at the top-level of the disk drive
    • %us_path%\core\perl: Sets the link path for example
  • popd: Restore calling address and returns.

Test 1 - Apache running as a standard program

  • Run link_create.bat (right click on file link_create.bat and run as administrator) allow program to make changes. This creates a symbolic link to the Perl executable.
  • Start UniController. From the Perl tab run Force Unix Shebang you only need to perform this once.
  • From UniController start Apache and check your Perl scripts run.
  • Close Apache and UniController.

Test 2 - Apache running as a service

  • Start UniService install and run Apache.
  • Again check your Perl scripts run.

Note: To remove the link double click on file link_delete.bat

Move Server

You can move Uniform Server to a new location. First run link_delete.bat to remove the symbolic link. Move folder UniServerZ and all its content to the new location. Run link_create.bat (right click file link_create.bat run as administrator) to create the symbolic link. You can now start the servers either as a standard program or service.