diff --git a/cli/src/main/java/org/jboss/as/cli/CommandLineMain.java b/cli/src/main/java/org/jboss/as/cli/CommandLineMain.java
index f33cde5..fa417eb 100644
--- a/cli/src/main/java/org/jboss/as/cli/CommandLineMain.java
+++ b/cli/src/main/java/org/jboss/as/cli/CommandLineMain.java
@@ -251,22 +251,26 @@ public class CommandLineMain {
 
         if(argError != null) {
             System.err.println(argError);
+            System.exit(1);
             return;
         }
 
         if(version) {
             final CommandContextImpl cmdCtx = new CommandContextImpl();
             VersionHandler.INSTANCE.handle(cmdCtx);
+            System.exit(0);
             return;
         }
 
         if(file != null) {
-            processFile(file, defaultControllerHost, defaultControllerPort, connect);
+            int exitCode = processFile(file, defaultControllerHost, defaultControllerPort, connect);
+            System.exit(exitCode);
             return;
         }
 
         if(commands != null) {
-            processCommands(commands, defaultControllerHost, defaultControllerPort, connect);
+            int exitCode = processCommands(commands, defaultControllerHost, defaultControllerPort, connect);
+            System.exit(exitCode);
             return;
         }
 
@@ -297,23 +301,25 @@ public class CommandLineMain {
                 " 'help' for the list of supported commands.");
         }
 
+        int exitCode = 0;
         try {
             while (!cmdCtx.terminate) {
                 final String line = console.readLine(cmdCtx.getPrompt());
                 if(line == null) {
                     cmdCtx.terminateSession();
                 } else {
-                    processLine(cmdCtx, line.trim());
+                    exitCode = processLine(cmdCtx, line.trim());
                 }
             }
         } finally {
             cmdCtx.disconnectController();
         }
-        System.exit(0);
+        System.exit(exitCode);
     }
 
-    private static void processCommands(String[] commands, String defaultControllerHost, int defaultControllerPort, final boolean connect) {
+    private static int processCommands(String[] commands, String defaultControllerHost, int defaultControllerPort, final boolean connect) {
 
+        int exitCode = 0;
         final CommandContextImpl cmdCtx = new CommandContextImpl();
         SecurityActions.addShutdownHook(new Thread(new Runnable() {
             @Override
@@ -335,7 +341,10 @@ public class CommandLineMain {
 
         try {
             for (int i = 0; i < commands.length && !cmdCtx.terminate; ++i) {
-                processLine(cmdCtx, commands[i]);
+                int code = processLine(cmdCtx, commands[i]);
+                if( code != 0 ) {
+                    exitCode = code;
+                }
             }
         } finally {
             if (!cmdCtx.terminate) {
@@ -343,10 +352,12 @@ public class CommandLineMain {
             }
             cmdCtx.disconnectController(!connect);
         }
+        return exitCode;
     }
 
-    private static void processFile(File file, String defaultControllerHost, int defaultControllerPort, final boolean connect) {
+    private static int processFile(File file, String defaultControllerHost, int defaultControllerPort, final boolean connect) {
 
+        int exitCode = 0;
         final CommandContextImpl cmdCtx = new CommandContextImpl();
         SecurityActions.addShutdownHook(new Thread(new Runnable() {
             @Override
@@ -377,6 +388,7 @@ public class CommandLineMain {
         } catch (Exception e) {
             cmdCtx.printLine("Failed to process file '" + file.getAbsolutePath() + "'");
             e.printStackTrace();
+            exitCode = 1;
         } finally {
             StreamUtils.safeClose(reader);
             if (!cmdCtx.terminate) {
@@ -384,14 +396,16 @@ public class CommandLineMain {
             }
             cmdCtx.disconnectController(!connect);
         }
+        return exitCode;
     }
 
-    protected static void processLine(final CommandContextImpl cmdCtx, String line) {
+    static int processLine(final CommandContextImpl cmdCtx, String line) {
+        int exitCode = 0;
         if (line.isEmpty()) {
-            return;
+            return 0;
         }
         if (line.charAt(0) == '#') {
-            return; // ignore comments
+            return 0; // ignore comments
         }
         if(isOperation(line)) {
             cmdCtx.setArgs(null, line, null);
@@ -409,6 +423,7 @@ public class CommandLineMain {
                     cmdCtx.printLine("#" + batch.size() + " " + batchedCmd.getCommand());
                 } catch (CommandFormatException e) {
                     cmdCtx.printLine(e.getLocalizedMessage());
+                    exitCode = 1;
                 }
             } else {
                 cmdCtx.operationHandler.handle(cmdCtx);
@@ -441,6 +456,7 @@ public class CommandLineMain {
                             cmdCtx.printLine("#" + batch.size() + " " + batchedCmd.getCommand());
                         } catch (CommandFormatException e) {
                             cmdCtx.printLine("Failed to add to batch: " + e.getLocalizedMessage());
+                            exitCode = 1;
                         }
                     }
                 } else {
@@ -448,6 +464,7 @@ public class CommandLineMain {
                         handler.handle(cmdCtx);
                     } catch (CommandFormatException e) {
                         cmdCtx.printLine(e.getLocalizedMessage());
+                        exitCode = 2;
                     }
                 }
 
@@ -455,8 +472,10 @@ public class CommandLineMain {
             } else {
                 cmdCtx.printLine("Unexpected command '" + line
                         + "'. Type 'help' for the list of supported commands.");
+                exitCode = 3;
             }
         }
+        return exitCode;
     }
 
     protected static jline.ConsoleReader initConsoleReader() {
