Bash: Hunk Failed


Recently we have been using some .sh scripts in bash, and we have frequently run into a Hunk failed error. The error usually comes up when you try to execute a .sh script in bash and it will look something like this:

There is almost no information available on the internet as how to solve these hunk failed errors, and the reason is that they are specific for each .sh script. There simply isn’t an all in one solution that works in all scripts like there is for PHP or Magento errors. However, by understanding what goes wrong in the script you can try to determine what gives the hunk failed error and solve it accordingly on your own.

Typically, the .sh scripts that may get this error are patch scripts. They expect a certain piece of code at specific line numbers that the script can replace. When the code is missing or another code is at that line number than the script expected, this error will be given. I will try to explain this through an example.

Recently we have had to patch some Magento sites with the new released security patch. With most sites, this went perfectly as you should expect. The patch merely edited some core files that you shouldn’t have edited anyway. However, sometimes we got the hunk failed error message, even on core files that weren’t edited. The reason for the error was that the line spacing was changed because of different hosting, editors or operating systems. When we downloaded the same exact core files for that particular Magento version fresh from the Magento site and replaced them with the old core files, the patch worked perfectly.

The code that the .sh file should replace/patch has been set hard in the code of the .sh file. Therefore, when it can’t find the exact code it’s looking for at the exact line number it’ll immediately throw the hunk failed error message. Restore the file that the .sh file try’s to edit to its original state and the error should now be solved.


Leave A Reply