You can override the built-in sort command in Windows by creating a batch file named sort.cmd that calls Git Bash. Place this file in a directory that appears earlier in your system PATH than C:\Windows\System32. This ensures your custom script executes instead of the native utility during installation processes.
Understanding the Command Resolution Order
When you type a command into the Windows Command Prompt or PowerShell, the operating system searches for an executable file based on the order defined in thePATH environment variable. The search proceeds from left to right. If you have a directory at the beginning of this list, any executable or batch file placed there will take precedence over files located in standard system directories like C:\Windows\System32.
The native sort.exe resides in C:\Windows\System32. To intercept calls to sort, you do not need to modify system files or change registry keys. Instead, you rely on the standard path resolution mechanism. By placing a custom script with the name sort.cmd or sort.bat in a user-controlled directory that is listed first in PATH, you effectively shadow the original command. This technique is commonly used in development environments to ensure tools like Git are utilized over legacy Windows utilities.
Creating the Override Script
The most robust way to replacesort with Git's implementation is to use a batch file that invokes git-bash.exe or directly calls the Git binary. Git provides its own version of coreutils, including sort, which often behaves more consistently across different platforms compared to the Windows native version.
Create a new text file and name it sort.cmd. It is critical to use the .cmd extension rather than .bat for modern compatibility, although both work. Open this file in a text editor and add the following content:
@echo off
"C:\Program Files\Git\usr\bin\sort.exe" %*
This script does two things. First, @echo off suppresses the display of the command being executed, keeping the output clean. Second, it calls the actual sort.exe binary provided by Git. The %* placeholder passes all arguments from the original call to the Git executable. This ensures that flags, input files, and redirection operators are handled correctly.
If you prefer using the full Git Bash shell for complex scenarios, you could also use:
@echo off
start "" "C:\Program Files\Git\git-bash.exe" -c "sort %*" && exit /b %errorlevel%
However, calling the binary directly is faster and less prone to window pop-ups during automated installations. Note that you must adjust the path to sort.exe if your Git installation is located in a different directory, such as C:\Users\YourName\AppData\Local\Programs\Git.
Configuring the PATH Variable
Once the script is created, you must ensure Windows finds it before the systemsort.exe. You need to add the folder containing sort.cmd to the beginning of your PATH variable.
1. Press Win + R, type sysdm.cpl, and press Enter to open System Properties.
2. Click the Advanced tab and select Environment Variables.
3. Under System variables (for all users) or User variables (for your account), find PATH and click Edit.
4. Click New and enter the full path to the directory where you saved sort.cmd.
5. Use the Move Up button repeatedly until this new entry is at the very top of the list.
It is important to note that changing the PATH only affects new processes. Existing command prompt windows will continue to use the old path order. You must close and reopen your terminal for the changes to take effect. If you are running an installer that spawns its own command line environment, ensure that environment inherits the updated PATH. Often, installers run in a fresh context, so they will pick up the new definition immediately if the system variable was modified correctly.
Verifying the Change
To confirm that your override is working, open a new Command Prompt window and type:where sort
The output should show the path to your sort.cmd file first, followed by the system sort.exe further down the list. You can test functionality by piping some text through it:
echo -e "banana\napple\ncherry" | sort
If the output is alphabetically sorted, your configuration is successful. This method allows you to maintain a consistent toolset for scripts that depend on Unix-like sorting behavior, which is particularly useful when working with cross-platform projects or Git repositories. For more details on how Git Bash interacts with Windows Terminal, see our guide on What's the difference between git-bash.exe and bash.exe and how they interact with Windows Terminal?. Additionally, if you encounter issues with batch files due to special characters, check Batch File Not Working with Space in File Name for troubleshooting tips.
Frequently Asked Questions
Will this break other programs that rely on the native sort command?
Most modern applications use their own internal libraries or explicitly call full paths to executables, so they will not be affected. However, older scripts that blindly assume sort is the Windows version might behave differently if they expect specific non-standard outputs. Always test critical workflows after making this change.
How do I revert this change if something goes wrong?
You can simply delete the sort.cmd file or move it out of your PATH directory. Alternatively, remove the custom directory from the top of the PATH variable in Environment Variables. Restarting your computer or opening a new terminal session will restore the default behavior.
Can I do this without modifying the system PATH?
Not easily for global commands. You could create a shortcut or alias in PowerShell, but that only works within that shell. For command-line installers and batch jobs, modifying the PATH is the most reliable method because it applies to all child processes spawned by the system.
Why does my installer still use the old sort command?
The installer process likely started before you changed the PATH or inherited the old environment variables. Ensure you restart the installer application after updating the environment variables. Also, verify that the installer is not hardcoding the path to C:\Windows\System32\sort.exe.
Is it safe to edit files in C:\Windows\System32?
No, you should never replace or rename system files in C:\Windows\System32. Doing so can corrupt your Windows installation and prevent updates from installing correctly. Using the PATH shadowing technique is the safe, supported alternative.