Q(uick)BASIC Statement: SHELL

Quick View

SHELL

A statement that suspends execution of a BASIC program, runs aDOS command or batch file (QB 1.1).COM, .EXE, or .BAT program, or a DOS command (QB 4.5).COM, .EXE, .BAT or .CMD program, or a DOS or OS/2 command (QB 7.1)and resumes execution of the BASIC program at the statement following the SHELL statement

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • SHELL [commandstring$]
Description/Parameter(s)
commandstring$ The name of a DOS command or batch file.
  • Your program resumes when the DOS command or batch file completes.
  • If you omit the command string, SHELL invokes a DOS shell and displays the DOS prompt. Use the EXIT command to resume your program.
Example
SHELL "DIR"
Syntax
  • SHELL [commandstring]
Description/Parameter(s)

The commandstring must be a valid string expression, containing the name of a program to run and any program options.

SHELL statement is called a "child process." Child processes are executed by the SHELL statement, loading and running a copy of COMMAND.COM with the /C option automatically; this option allows any parameters in commandstring to be passed to the child process. It also allows redirection of standard input and output, and execution of built-in commands such as DIR, PATH, and SORT.

The program name in commandstring may have any extension you wish. If no extension is supplied, COMMAND.COM looks for a .COM file, then an .EXE file, and finally, a .BAT file. If COMMAND.COM is not found, SHELL issues an error message that reads "File not found." BASIC does not generate an error if COMMAND.COM cannot find the file specified in commandstring.

Any text separated from the program name by at least one blank is treated as program parameters by COMMAND.COM.

BASIC remains in memory while the child process is running. When the child process finishes, BASIC continues.

SHELL with no commandstring gives you a new COMMAND.COM shell. You may now do anything that COMMAND.COM allows. Enter the DOS command EXIT when you are ready to return to BASIC.

Examples

The following example shows how a single SHELL statement starts up a new COMMAND.COM:

SHELL 'Get a new COMMAND.COM.

Sample Output:

The IBM Personal Computer DOS Version 3.20 (C)Copyright International Business Machines Corp 1981, 1986 (C)Copyright Microsoft Corp 1981, 1986 D:\QB4>

SHELL_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the SHELL statement. To look at the program in the View window and, optionally, to run it, load the program using the File menu's Open Program command.
The program copies all files modified on a certain date from a specified directory.

Syntax
  • SHELL (commandstring$)
Description/Parameter(s)
  • The argument commandstring$ is a string expression that contains a valid DOS or OS/2 command or the name of a .COM, .EXE, .BAT, or .CMD program and any program arguments.

Programming With OS/2 Protected Mode

  • If you are using OS/2, make sure that the SET COMSPEC configuration command in your CONFIG.SYS file specifies the path for the CMD.EXE file. If the path for CMD.EXE is not set, you will get an error message when SHELL searches for the CMD.EXE file.

Usage Notes

  • Any .COM file, .EXE file, .BAT program, .CMD program, DOS command, or OS/2 command that runs under the SHELL statement is called a "child process." Child processes are executed by the SHELL statement, which loads and runs a copy of COMMAND.COM (for DOS) or CMD.EXE (for OS/2) with the /C option.
  • The /C option allows any parameters in commandstring$ to be passed to the child process. It also allows redirection of standard input and output, and execution of built-in commands such as DIR and PATH.
  • The program name in commandstring$ can have any extension you wish. If no extension is supplied, COMMAND.COM (for DOS) or CMD.EXE (for OS/2) looks for a .COM file, then an .EXE file, and finally, a .BAT file (for DOS) or a .CMD file (for OS/2). If COMMAND.COM or CMD.EXE is not found, SHELL issues the error message, "File not found." BASIC does not generate an error if COMMAND.COM or CMD.EXE cannot find the file specified in commandstring$, but the operating system will generate an error message.
  • COMMAND.COM or CMD.EXE treat as program parameters any text separated from the program name by at least one blank.
  • BASIC remains in memory while the child process is running. When the child process finishes, BASIC continues.
  • SHELL with no commandstring$ gives you a new shell (COMMAND.COM for DOS or CMD.EXE for OS/2). You then can enter operating-system commands at the prompt. Use the EXIT command to return to BASIC.
Examples

This example uses the SHELL statement to start up a new COMMAND.COM. Type EXIT at the operating system prompt to return to QBX.

SHELL

This example uses an argument with the SHELL statement to execute a DOS command. When the program is finished, you are returned to QBX. The statement illustrates how you can quickly see a directory listing to check the creation time of some file.

SHELL ("DIR | MORE")