I am trying to replace all occurrences of <value>*</value> into <value>file:\\</value> in a designated file test.txt.

I work on Windows and use sed I have installed with Win-32 compiled set of bash utils.

I use a following command:

sed -n "s/<value>.*<\/value>/<value>file:\\\\<\/value>/g" "test.txt" > "test.txt.new"

I want to run this command as a part of Wpkg script.


When I use this command from commandline, everything works properly.

But when I use this command via Wpkg script:

<install cmd='%BASH%\sed -n "s/&lt;value&gt;.*&lt;\/value&gt;/&lt;value&gt;file:\\\\&lt;\/value&gt;/g;" test.txt" > "test.txt new"' />

...then what I get is:

Exit code returned non-successful value (2) on command '%BASH%\sed -n "s/<value>
.*<\/value>/<value>file:\\\\<\/value>/g;" "test.txt" > "test.txt.new"'.

The above means that sed application returned exit code 2. Additionaly, the new file has not been created.

I browsed google for a really long time and to my surprise, sed seems to have a pretty poor documentation. I have found no documentation regarding this exit code.


Anyone got a clue what does exit code 2 mean for sed?

...or an alternative solution?

(must be unattended, pretty sure might use common bash tools)


I'm not convinced that your problem is with sed. Is %BASH% supposed to evaluate to a directory in which the sed executable resides and have you confirmed that it does so? Because my expectation would be that (if defined at all on Windows) %BASH% would evaluate to the path to the bash executable itself, in which case appending "\sed" onto it makes no sense.

In particular I'll note that the DOS error code 2 is "File Not Found" - perhaps it's an issue with finding the file "%BASH%\sed"?

  • %BASH% is defined properly, but... "2" being a DOS error code, rather than sed's, is a great tip. I wonder if it might be the lack of .exe extension, that's really causing the problem :) I'll check soon, thanks! – Michał Sacharewicz Mar 22 '15 at 9:42
  • I've encountered problems when using Posix utils (e.g. 'find') on Windows from .bat/.cmd files, because the commands output and expect *nix-style paths. You don't appear to have that in your expression, but I've never used Wpkg - is it looking for *nix style paths even on Windows? – fencepost Mar 23 '15 at 5:57
  • If I were to run sed from within bash, I would have to use *nix style paths indeed. But, when run using cmd, standard Windows paths work properly. – Michał Sacharewicz Mar 23 '15 at 9:31

Does sed issue an exit code?

Most versions of sed do not, but check the documentation that came with whichever version you are using. GNU sed issues an exit code of 0 if the program terminated normally, 1 if there were errors in the script, and 2 if there were errors during script execution.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.