lang • shell


[ Process = ] SHELL Command [ WAIT ] [ FOR { { READ | INPUT } | { WRITE | OUTPUT } } ] [ AS Name ]
SHELL Command TO Variable

Executes a command by running a child process through a system shell.

An internal Process object is created to manage the command.

Standard syntax

The command is a string containing a command passed to the system shell (/bin/sh).

You can get a reference to the internal Process object created by using an assignment.

Quick syntax

If you use the second syntax, the command is executed, the interpreter waiting for its end, and the complete command output is put in the specified string.

You have no control on the executed process.

Only the standard output of the process is retrieved. The error output is not redirected.

If you need to mix both output, use the shell redirection syntax:

Shell "command 2>&1" To Result


You can specify new environment variables for the running process by using the WITH keyword just after the command argument:

[ Process = ] SHELL Command WITH Environment ...

Environment is an array of strings, each string having the following form: "NAME=VALUE". NAME is the name of the environment variable, VALUE is its value.

If you want to erase an environment variable, just use the string "NAME=".

Running Inside A Virtual Terminal

If the process is run inside a virtual terminal, i.e. if you use the FOR INPUT / OUTPUT syntax, then you can send control characters to the process standard input to get the same effect as if you enter them inside a real terminal. ^C stops the process, ^Z suspends it, and so on.

A virtual terminal has only one output. Consequently, the standard error output of the running process is received through the Read event.

Some programs have a command-line interface that is accessible only if running inside a virtual terminal.

If you plan to control an application by sending commands to standard input then testing should be performed outside of the IDE (i.e. make an executable and launch it from the command line) as the console within the development environment is not a true virtual terminal and will cause unexpected results.

Specifying The Shell

Since 3.1

You can specify which shell is used for running the command with the System.Shell property.

By default, the shell command is run through /bin/sh.

Argument quoting

Since arguments are sent to a shell, you have to quote them, as if you were typing the command in a terminal screen.

SHELL "perl -e 'print while <>;'" FOR READ WRITE

Or you can use the Shell$ function to create a quoted string that won't be modified by the shell.


' Get the contents of a directory and print it to the standard output
Shell "ls -la /tmp" Wait
' Get the contents of a directory to a string
Dim Result As String

Shell "ls -la /tmp" To Result
' Get the contents of a directory in background

Dim Result As String

Shell "ls -la /tmp" For Read As "Process"


Public Sub Process_Read()

  Dim sLine As String

  sLine = Read #Last, -256

  Result &= sLine
  Print sLine;


If you want to know how many bytes you can read in a Process_Read event handler, use the Lof function.

Unlike the VB Shell command, which returns a process ID and relies on the programmer to make API calls to control the process, the Gambas Shell function optionally returns a Process object (if used as an assignment to a variable declared AS) which can be used to directly kill or otherwise control the spawned process. Additionally, the process may be run synchronously or asynchronously, in contrast to the VB equivalent.

See also