shell (API)

From ComputerCraft Wiki
Jump to: navigation, search

The shell API allows you to interface with ComputerCraft's shell - CraftOS. The shell API is only available when programs are ran from the shell or using shell.run/shell.openTab.

Note that this "API" is not loaded into _G using os.loadAPI() - instead, it's loaded by the shell script itself into a separate user environment, one which also holds global variables for any other scripts the user executes. The multishell API is handled in the same manner. They can thus be easily sandboxed (allowing for multiple shells to run on the same system at once - the APIs for each being able to return relevant results for each instance), though it does mean other APIs can't refer to these functions (as they lack access to the environment table dedicated to user scripts).

API

Grid disk.png  shell (API)
Function Return values Description
shell.exit() nil Exits the current shell.
shell.dir() string directory Returns the path to the working directory.
shell.setDir(string path) nil Sets the working directory.
shell.path() string path Returns the path.
shell.setPath(string path) nil Sets the path.
shell.resolve(string localPath) string absolutePath Resolves a local path to an absolute path.
shell.resolveProgram(string name) string absolutePath Resolves the absolute path to the program whose name you provided.
shell.aliases() table aliases Returns aliases.
shell.setAlias(string alias, string program) nil Sets an alias for program.
shell.clearAlias(string alias) nil Clears an alias.
shell.programs([boolean showHidden]) table programs Returns a table of files in the current directory and in all paths in shell.path.
shell.getRunningProgram() string path Returns the absolute path to the currently-executing program.
shell.run(string command [, string args1, string args2, ...]) boolean success Runs a command (program).
shell.openTab(string command [, string args1, string args2, ...]) number tabID Runs a program in another multishell tab. Requires version 1.6 or newer and an advanced system.
shell.switchTab(number tabID) nil Switches the multishell tab to tab with the given ID. Requires version 1.6 or newer and an advanced system.
shell.complete(string prefix) table completionList Given a partial command line, returns a list of suffixes that could potentially be used to complete it. Requires version 1.74 or newer.
shell.completeProgram(string prefix) table completionList Given a partial script / directory path, returns a list of suffixes that could potentially be used to complete it, including alias and path matches. Requires version 1.74 or newer.
shell.setCompletionFunction(string path, function completionFunction) nil Registers a function that determines how shell.complete() handles completion behavior for a given script. Requires version 1.74 or newer.
shell.getCompletionInfo() table completionFunctions Returns a pointer to the table containing functions registered by shell.setCompletionFunction() for use with shell.complete(). Requires version 1.74 or newer.

Note that shell.run and shell.openTab concatenate any arguments they are given (with single space separator characters) before parsing the command, and as of ComputerCraft version 1.45, now also support a single string containing all parameters.