Class

# Shell

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to execute Unix or DOS <span class="title-ref">Shell</span> commands under Windows, macOS or Linux.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                             | Type                               | Read-Only | Shared |
|----------------------------------|------------------------------------|-----------|--------|
| `Arguments<shell.arguments>`     | `String</api/data_types/string>`   |           |        |
| `Backend<shell.backend>`         | `String</api/data_types/string>`   |           |        |
| `Canonical<shell.canonical>`     | `Boolean</api/data_types/boolean>` |           |        |
| `ExecuteMode<shell.executemode>` | `ExecuteModes<shell.executemodes>` |           |        |
| `ExitCode<shell.exitcode>`       | `Integer</api/data_types/integer>` |           |        |
| `IsRunning<shell.isrunning>`     | `Boolean</api/data_types/boolean>` |           |        |
| `PID<shell.pid>`                 | `Integer</api/data_types/integer>` |           |        |
| `Result<shell.result>`           | `String</api/data_types/string>`   |           |        |
| `TimeOut<shell.timeout>`         | `Integer</api/data_types/integer>` |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                         | Parameters                                                                                  | Returns                          | Shared |
|------------------------------|---------------------------------------------------------------------------------------------|----------------------------------|--------|
| `Close<shell.close>`         |                                                                                             |                                  |        |
| `Execute<shell.execute>`     | command As `String</api/data_types/string>`                                                 |                                  |        |
|                              | command As `String</api/data_types/string>`, parameters As `String</api/data_types/string>` |                                  |        |
| `Poll<shell.poll>`           |                                                                                             |                                  |        |
| `ReadAll<shell.readall>`     |                                                                                             | `String</api/data_types/string>` |        |
| `Write<shell.write>`         | s As `String</api/data_types/string>`                                                       |                                  |        |
| `WriteLine<shell.writeline>` | s As `String</api/data_types/string>`                                                       |                                  |        |

## Events

<div class="rst-class">

table-centered_column_4

</div>

| Name                                 | Parameters | Returns |
|--------------------------------------|------------|---------|
| `Completed<shell.completed>`         |            |         |
| `DataAvailable<shell.dataavailable>` |            |         |

## Enumerations

<div id="shell.executemodes">

<div class="rst-class">

forsearch

</div>

</div>

Shell.ExecuteModes

### ExecuteModes

> Specifies the types of modes in which the <span class="title-ref">Shell</span> can execute.
>
> | Enum         | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
> |--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
> | Synchronous  | (Default) The <span class="title-ref">shell</span> executes its command and returns the result in the Result property when the script has finished running. Synchronous <span class="title-ref">shells</span> block the main UI thread, even when they are in a thread themselves. For long-running <span class="title-ref">shell</span> processes, use one of the other <span class="title-ref">shell</span> modes instead.                                                                                                                                                             |
> | Asynchronous | The <span class="title-ref">shell</span> executes its command and returns data via the `DataAvailable<shell.dataavailable>` event. It does not wait for a command to finish before executing the next command. An asynchronous <span class="title-ref">shell</span> script can run in the background. You'll need to make sure the <span class="title-ref">Shell</span> instance does not go out of scope while you are waiting for operations to complete.                                                                                                                              |
> | Interactive  | The script can display a prompt and the user can interact with the <span class="title-ref">shell</span> script while it is running. Data can be sent to a running <span class="title-ref">shell</span> session with the Write method and data is returned via the `DataAvailable<shell.dataavailable>` event. Refer the interactive <span class="title-ref">shell</span> Example included with your installation (Advanced/Shell). You'll need to make sure the <span class="title-ref">Shell</span> instance does not go out of scope while you are waiting for operations to complete. |

## Property descriptions

<div id="shell.arguments">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Arguments

**Arguments** As `String</api/data_types/string>`

> Specifies the arguments to pass to the <span class="title-ref">Shell</span> backend. The default is `-c`.

<div id="shell.backend">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Backend

**Backend** As `String</api/data_types/string>`

> Changes the <span class="title-ref">Shell</span> backend that is used.
>
> The default on macOS and Linux is `bash`; on Windows `cmd`. macOS/Linux can also use `bourne`. Windows can also use `powershell`.
>
> Change the <span class="title-ref">Shell</span> backend to Bourne <span class="title-ref">Shell</span> (if available):
>
> ``` xoj
> Var sh As New Shell
>
> sh.Backend = "bourne"
> sh.Execute("ls")
>
> TextArea1.Text = sh.Result
> ```

<div id="shell.canonical">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Canonical

**Canonical** As `Boolean</api/data_types/boolean>`

> `True</api/language/true>` to allow control characters to be sent to the <span class="title-ref">Shell</span>.
>
> Use this property to support special control characters like `Ctrl A` (go to first line), and `Ctrl E` (go to last line), etc. which normally just transmit as actual binary data to the <span class="title-ref">Shell</span>.
>
> `False</api/language/false>` (default) means current behavior (i.e. receiving input without the need for a new-line), but if enabled then the <span class="title-ref">Shell</span> will operate in Canonical mode (i.e. waiting on input until a new-line is received which allows these special character to work). The Canonical mode must be set before calling Execute to be effective, just like the <span class="title-ref">Shell</span>.Mode property.

<div id="shell.executemode">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.ExecuteMode

**ExecuteMode** As `ExecuteModes<shell.executemodes>`

> Controls the mode in which the <span class="title-ref">Shell</span> operates. The <span class="title-ref">Shell</span> can be running in Synchronous, Asynchronous, or Interactive modes.
>
> To access the `DataAvailable<shell.dataavailable>` event handler, you can subclass <span class="title-ref">Shell</span> or use `AddHandler</api/language/addhandler>`.
>
> The following terminal application allows you to submit Unix commands using the interactive mode. The interface consists of two `TextFields</api/user_interface/desktop/desktoptextfield>`, InputField, in which the user can enter a command, and OutputField that displays the results.
>
> The `Opening<desktopwindow.opening>` event for the window initializes the <span class="title-ref">Shell</span> object (declared as a property of the window).
>
> ``` xojo
> mShell = New Shell
> mShell.ExecuteMode = Shell.ExecuteModes.Interactive
> ```
>
> The user can type a unix command into the `TextField</api/user_interface/desktop/desktoptextfield>`, InputField. When they press Return, the following code in the `TextField</api/user_interface/desktop/desktoptextfield>`'s KeyDown event runs. The Write method sends the command to the <span class="title-ref">Shell</span>'s input buffer.
>
> ``` xojo
> If Key = EndOfLine.CR Then
>
>   If Not mShell.IsRunning Then
>     mShell.Execute "sh"
>   End If
>
>   mShell.Write(InputField.Text)
>   mShell.Write(EndOfLine.CR)
>   InputField.Text = ""
>   Return True
>
> Else
>   Return False
> End If
> ```
>
> A `Timer</api/language/timer>` calls the ReadAll method in its Action event and displays the output in OutputField:
>
> ``` xojo
> If mShell <> Nil Then
>
>   Var output As String = mShell.ReadAll
>
>   If Not output.IsEmpty Then
>     OutputField.SelectedText = output
>   End If
>
> End If
> ```

<div id="shell.exitcode">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.ExitCode

**ExitCode** As `Integer</api/data_types/integer>`

> The exit code returned by the <span class="title-ref">Shell</span>.
>
> It returns `0` if the Execute method was executed successfully. Otherwise, it returns a system-supplied exit code. On Windows, it returns `-1` if execution fails.
>
> This example checks the value of exit code and prints it if it is not zero.
>
> ``` xojo
> Var sh As New Shell
>
> sh.Backend = TextField1.Text
> sh.Arguments = TextField2.Text
> sh.Execute TextField3.Text
> TextArea1.Text = sh.Result
>
> If sh.ExitCode <> 0 Then
>   MessageBox("The exit code is: " + sh.ExitCode.ToString)
> End If
> ```

<div id="shell.isrunning">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.IsRunning

**IsRunning** As `Boolean</api/data_types/boolean>`

> `True</api/language/true>` if an asynchronous or interactive <span class="title-ref">Shell</span> process is running.
>
> This example checks whether the <span class="title-ref">Shell</span> is running before trying to use it.
>
> ``` xojo
> If sh.IsRunning Then
>   sh.Poll
> End If
> ```

<div id="shell.pid">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.PID

**PID** As `Integer</api/data_types/integer>`

> The ID of the interactive <span class="title-ref">Shell</span> process.
>
> On Windows, this is the process handle rather than the ID of the process.

<div id="shell.result">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Result

**Result** As `String</api/data_types/string>`

> Contains the contents of the output buffer without clearing the buffer.
>
> This example runs the `ls` command in a <span class="title-ref">Shell</span> and displays the results:
>
> ``` xojo
> Var sh As New Shell
>
> sh.Execute("ls")
> TextArea1.Text = sh.Result
> ```

<div id="shell.timeout">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.TimeOut

**TimeOut** As `Integer</api/data_types/integer>`

> The time (in milliseconds) that specifies how long a process can run before it is automatically terminated (Windows only). The default is `2000` or `2` seconds. Use `-1` to indicate no timeout.
>
> Timeout is only used on Microsoft Windows.
>
> ``` xojo
> Var s As New Shell
>
> s.ExecuteMode = Shell.ExecuteModes.Synchronous
> s.TimeOut = 10000
> s.Execute("DIR")
>
> Select Case s.ExitCode
> Case -2
>   'the command timed out
> Case 0
>   'the command succeeded
> Else
>   'Something else happened
> End Select
> ```

## Method descriptions

<div id="shell.close">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Close

**Close**

> Shuts down the running command. Calling Close triggers the Completed event.
>
> ``` xojo
> Var sh As New Shell
> .
> .
> sh.Close
> ```

<div id="shell.execute">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Execute

**Execute**(command As `String</api/data_types/string>`)

**Execute**(command As `String</api/data_types/string>`, parameters As `String</api/data_types/string>`)

> Executes a one-line Unix or DOS <span class="title-ref">Shell</span> command.
>
> If you are in **Synchronous mode** (mode 0), the `Result<shell.result>` property will contain the results. The *command* parameter is the path/name of the executable to run and the second parameter contains the arguments to pass to the executable. You can specify the executable without passing a second parameter. On Windows, if the path/name of the executable contains spaces and you want to pass arguments to it, it is safer to separate the two.
>
> If you are in **Asynchronous mode** (mode 1 or 2), use the *Execute* method for the first command to be executed. In **Interactive mode** (mode 2), you can then later use `Write<shell.write>` or `WriteLine<shell.writeline>` to send data or new commands to the <span class="title-ref">Shell</span>.
>
> Execute the `ls` command in a <span class="title-ref">Shell</span> and display the results in a `TextArea</api/user_interface/desktop/desktoptextarea>`:
>
> ``` xojo
> Var sh As New Shell
>
> sh.Execute("ls")
> TextArea1.Text = sh.Result
> ```

<div id="shell.poll">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Poll

**Poll**

> Looks for data returned from the running <span class="title-ref">Shell</span> and may trigger a DataAvailable event.
>
> ``` xojo
> Var sh As New Shell
> .
> .
> sh.Poll
> ```

<div id="shell.readall">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.ReadAll

**ReadAll** As `String</api/data_types/string>`

> Returns the contents of the output buffer and clears the buffer. As a consequence, the property `Result<shell.result>` will return an empty string.

<div id="shell.write">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Write

**Write**(s As `String</api/data_types/string>`)

> Sends the passed string to the <span class="title-ref">Shell</span>'s input buffer.

<div id="shell.writeline">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.WriteLine

**WriteLine**(s As `String</api/data_types/string>`)

> Sends the passed string ending in a linefeed to the <span class="title-ref">Shell</span>'s input buffer.

## Event descriptions

<div id="shell.completed">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.Completed

**Completed**

> Called only in modes Asynchronous and Interactive. Triggered whenever the executed command is finished or the Close method is called.

<div id="shell.dataavailable">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

Shell.DataAvailable

**DataAvailable**

> Occurs when additional data has come into the internal receive buffer.

## Notes

Use the <span class="title-ref">Shell</span> class to execute DOS or Unix commands and get the results. The `Execute<shell.execute>` method executes a one-line command in Synchronous mode. This causes two properties of the <span class="title-ref">Shell</span> object to change: `ExitCode<shell.exitcode>`, which is a system-supplied error code or 0 for no error; and `Result<shell.result>`, which is a `string</api/data_types/string>` containing the output of the command. The `TimeOut<shell.timeout>` property specifies how long (in milliseconds) a process can run before it is automatically terminated. A value of -1 means the process can run indefinitely. This property currently applies only to Windows.

The process running in the <span class="title-ref">Shell</span> is killed when the object gets out of scope even if running in asynchronous or interactive modes.

The <span class="title-ref">Shell</span> is not equivalent to the Terminal or Command app for your OS. Paths and other default settings will likely not be the same. If you need to do configuration of the <span class="title-ref">Shell</span> before you use it, be sure to set it up to be interactive so you can set up the configuration before calling other <span class="title-ref">Shell</span> commands. Alternatively you could create a batch file that sets everything up and call that instead.

<div class="warning">

<div class="title">

Warning

</div>

If you send a unavailable command to the <span class="title-ref">Shell</span>, a `ShellNotAvailableException</api/exceptions/shellnotavailableexception>` will be raised.

</div>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### <span class="title-ref">Shell</span> differences on windows

It appears that some Windows <span class="title-ref">Shell</span> commands such as ftp or telnet will not work as expected due to these tools not using the standard IO streams that can be read by the <span class="title-ref">Shell</span> class.

Some workarounds:

- Take advantage of the ftp commands built-in scripting capability.
- Spawn the ftp app in a hidden console window and read its input using low-level console IO functions as described in this Microsoft document: <http://msdn.microsoft.com/en-us/library/ms684965(VS.85>).aspx
- Use a 3rd part ftp or telnet class.

## Sample code

Using the synchronous mode, the following code lists the current directory's files using the `dir` command on Windows and `ls` on macOS and Linux.

``` xojo
Var s As New Shell

#If TargetWindows Then
  s.Execute("dir")
#ElseIf TargetMacOS Or TargetLinux Then
  s.Execute("ls -la")
#EndIf

If s.ExitCode = 0 Then
  TextField1.Text= s.Result
Else
  MessageBox("Error code: " + s.ExitCode.ToString)
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following example gets the names and values of the environment variables on the user's computer and displays the information in a `TextField</api/user_interface/desktop/desktoptextfield>`. You can use the `EnvironmentVariable<system.environmentvariable>` method of the `System</api/os/system>` module to get or set individual environment variables.

``` xojo
Var s As New Shell
Var cmd As String

#If TargetMacOS Or TargetLinux Then
  cmd = "env"
#ElseIf TargetWindows Then
  cmd = "set"
#EndIf

s.Execute(cmd)

If s.ExitCode = 0 Then
  TextField1.Text = s.Result
Else
  TextField1.Text = "Error " + s.ExitCode.ToString
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following terminal application allows you to submit Unix commands using the interactive mode. The interface consists of two `TextFields</api/user_interface/desktop/desktoptextfield>`, InputField, in which the user can enter a command, and OutputField that displays the results.

The Opening event for the window initializes the <span class="title-ref">Shell</span> object (declared as a property of the window).

``` xojo
mShell = New Shell
mShell.ExecuteMode = Shell.ExecuteModes.Interactive
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The user can type a unix command into the `TextField</api/user_interface/desktop/desktoptextfield>`, InputField. When he presses Return, the following code in the `TextField</api/user_interface/desktop/desktoptextfield>`'s KeyDown event runs. The Write method sends the command to the <span class="title-ref">Shell</span>'s input buffer.

``` xojo
If key = EndOfLine.CR Then

  If Not mShell.IsRunning Then
    mShell.Execute("sh")
  End If

  mShell.Write(InputField.Text)
  mShell.Write(EndOfLine.CR)
  InputField.Text = ""
  Return True

Else
  Return False
End If
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

A `Timer</api/language/timer>` calls the ReadAll method in its Action event and displays the output in OutputField:

``` xojo
If mShell <> Nil Then

  Var output As String = mShell.ReadAll

  If Not output.IsEmpty Then
    OutputField.SelectedText = output
  End If

End If
```

## Compatibility

|                       |                       |
|-----------------------|-----------------------|
| **Project Types**     | Console, Desktop, Web |
| **Operating Systems** | All                   |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `System.EnvironmentVariable<system.environmentvariable>`; `TargetLinux</api/compiler_directives/targetlinux>`, `TargetMachO</api/compiler_directives/targetmacho>`, `TargetMacOS</api/compiler_directives/targetmacos>`, `TargetWindows</api/compiler_directives/targetwindows>`, `TargetX86</api/compiler_directives/targetx86>` constants.

</div>
