21

I have written a Vim plugin which shells out to run external commands. Two of the commands I run are diff and grep which can each exit with a non-zero exit code during "normal" operation.

(diff exits with exit code 1 when it finds differences and grep exits with exit code 1 when it doesn't find matches.)

For the purposes of my Vimscript I need to return an exit code of 0 from these commands. So far I'm constructing the commands like this:

diff a b || true

And:

grep foo bar || true

This works for me on OS X and it apparently works for some Windows users. However, when I run Windows 7 on OS X via VirtualBox, using the Bash installed by the Git installer, I get the error message:

'true' is not recognized as an internal or external command, operable program or batch file.

What is the correct successful-no-op command to use on Windows?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andy Stewart
  • 5,013
  • 2
  • 28
  • 38
  • As a minor point, `true` and `false` are commands that return 0 (success) and non-zero(failure) to the OS. The need for actual command s are because, as far as I know, the traditional shells didn't have a literal way to specify a boolean. – Noufal Ibrahim Feb 26 '14 at 16:09
  • 1
    They aren't really needed, as by themselves they are identical to `(exit 0)` and `(exit 1)`, respectively. – chepner Feb 26 '14 at 16:11
  • @chepner That solved the problem for me; thanks! I now use `diff a b || exit 0`. If you can add your comment as an answer I'll be able to accept it. – Andy Stewart Feb 27 '14 at 08:44
  • 4
    When you say "on Windows", do you mean running bash under Windows, or running the Windows `cmd` shell? The answer is likely to be quite different. bash has `false` and `true` as built-in commands, and "`'true' is not recognized as an internal or external command, operable program or batch file.`" is a Windows cmd error message, not a bash error message. – Keith Thompson Nov 15 '17 at 23:50

4 Answers4

12
VER>NUL

works for me.

For example,

MKDIR . || VER>NUL

issues an error message, but it sets %ERRORLEVEL% to 0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user368683
  • 360
  • 3
  • 7
7

The context is a Windows cmd shell (used by the git-cmd.bat script):

Following "Exiting batch with EXIT /B X where X>=1 acts as if command completed successfully when using && or || operators between batch calls", you could define in your path a true.bat file with:

@%COMSPEC% /C exit 1 >nul
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
7
cd .

also sets %ERRORLEVEL% to 0 but runs a bit faster and writes a bit shorter than ver>nul. Example:

mkdir . 2>nul || cd .
6

true is roughly equivalent to (exit 0) (the parentheses create a subshell that exits with status 0, instead of exiting your current shell.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • 11
    This did not work on Win 10. When `(exit 0)` is called, either on its own or because of a fall through from a failed command, the shell exits. – Kenn Sebesta Apr 07 '17 at 22:30
  • If true, that would appear to be a fairly significant bug in Window's `bash` implementation. – chepner Apr 07 '17 at 23:09
  • 7
    @chepner: It's not entirely clear, but I believe the OP is looking for an equivalent in the Windows `cmd` shell. If the OP were using bash, no replacement should be necessary; bash has `false` and `true` as built-in commands, and that's not a bash error message. (I don't know what `(exit 0)` does in `cmd`.) – Keith Thompson Nov 15 '17 at 23:52
  • 2
    Did this on Win 10, had the terminal abruptly closed, and felt like the n00b on mIRC when they asked for a way to e.g. change the font color and some smarta** answered ALT+F4. "Tibo has left the chatroom". –  Aug 30 '19 at 08:11
  • 5
    This answer is wrong, paranthesis in `cmd` don't fork anything. `(exit 0)` will cause the script to exit –  Sep 26 '19 at 16:36
  • as others have explained this is not the correct answer. – objectNotFound Jan 16 '21 at 04:59
  • The OP accepted the answer, and seemed pretty clear about using `bash`. If you aren't using `bash` on Wndows, then no, this will not work. – chepner Jan 16 '21 at 13:46