shell
The shell API provides access to CraftOS's command line interface.
It allows you to start programs, add completion for a program, and much more.
shell is not a "true" API. Instead, it is a standard program, which injects
its API into the programs that it launches. This allows for multiple shells to
run at the same time, but means that the API is not available in the global
environment, and so is unavailable to other APIs.
Programs and the program path
When you run a command with the shell, either from the prompt or from Lua code, the shell API performs several steps to work out which program to run:
-
Firstly, the shell attempts to resolve aliases. This allows us to use multiple names for a single command. For example, the
listprogram has two aliases:lsanddir. When you writels /rom, that's expanded tolist /rom. -
Next, the shell attempts to find where the program actually is. For this, it uses the program path. This is a colon separated list of directories, each of which is checked to see if it contains the program.
listorlist.luadoesn't exist in.(the current directory), so the shell now looks in/rom/programs, wherelist.luacan be found! -
Finally, the shell reads the file and checks if the file starts with a
#!. This is a hashbang, which says that this file shouldn't be treated as Lua, but instead passed to another program, the name of which should follow the#!.
Changes
- Changed in version 1.103.0: Added support for hashbangs.
| execute(command, ...) | Run a program with the supplied arguments. |
|---|---|
| run(...) | Run a program with the supplied arguments. |
| exit() | Exit the current shell. |
| dir() | Return the current working directory. |
| setDir(dir) | Set the current working directory. |
| path() | Get the path where programs are located. |
| setPath(path) | Set the current program path. |
| resolve(path) | Resolve a relative path to an absolute path. |
| resolveProgram(command) | Resolve a program, using the program path and list of aliases. |
| programs([include_hidden]) | Return a list of all programs on the path. |
| complete(sLine) | Complete a shell command line. |
| completeProgram(program) | Complete the name of a program. |
| setCompletionFunction(program, complete) | Set the completion function for a program. |
| getCompletionInfo() | Get a table containing all completion functions. |
| getRunningProgram() | Returns the path to the currently running program. |
| setAlias(command, program) | Add an alias for a program. |
| clearAlias(command) | Remove an alias. |
| aliases() | Get the current aliases for this shell. |
| openTab(...) | Open a new multishell tab running a command. |
| switchTab(id) | Switch to the multishell tab with the given index. |
- execute(command, ...)Source
Run a program with the supplied arguments.
Unlike
shell.run, each argument is passed to the program verbatim. Whileshell.run("echo", "b c")runsechowithbandc,shell.execute("echo", "b c")runsechowith a single argumentb c.Parameters
Returns
booleanWhether the program exited successfully.
Usage
Run
paint my-imagefrom within your program:shell.execute("paint", "my-image")
Changes
- New in version 1.88.0
- run(...)Source
Run a program with the supplied arguments.
All arguments are concatenated together and then parsed as a command line. As a result,
shell.run("program a b")is the same asshell.run("program", "a", "b").Parameters
- ...
stringThe program to run and its arguments.
Returns
booleanWhether the program exited successfully.
Usage
Run
paint my-imagefrom within your program:shell.run("paint", "my-image")
See also
shell.executeRun a program directly without parsing the arguments.
Changes
- Changed in version 1.80pr1: Programs now get their own environment instead of sharing the same one.
- Changed in version 1.83.0:
argis now added to the environment.
- ...
- exit()Source
Exit the current shell.
This does not terminate your program, it simply makes the shell terminate after your program has finished. If this is the toplevel shell, then the computer will be shutdown.
- dir()Source
Return the current working directory. This is what is displayed before the
>of the shell prompt, and is used byshell.resolveto handle relative paths.Returns
stringThe current working directory.
See also
setDirTo change the working directory.
- setDir(dir)Source
Set the current working directory.
Parameters
- dir
stringThe new working directory.
Throws
If the path does not exist or is not a directory.
Usage
Set the working directory to "rom"
shell.setDir("rom")
- dir
- path()Source
Get the path where programs are located.
The path is composed of a list of directory names in a string, each separated by a colon (
:). On normal turtles will look in the current directory (.),/rom/programsand/rom/programs/turtlefolder, making the path.:/rom/programs:/rom/programs/turtle.Returns
stringThe current shell's path.
See also
setPathTo change the current path.
- setPath(path)Source
Set the current program path.
Be careful to prefix directories with a
/. Otherwise they will be searched for from the current directory, rather than the computer's root.Parameters
- path
stringThe new program path.
Changes
- New in version 1.2
- path
- resolve(path)Source
Resolve a relative path to an absolute path.
The
fsandioAPIs work using absolute paths, and so we must convert any paths relative to the current directory to absolute ones. This does nothing when the path starts with/.Parameters
- path
stringThe path to resolve.
Usage
Resolve
startup.luawhen in theromfolder.shell.setDir("rom") print(shell.resolve("startup.lua")) -- => rom/startup.lua
- path
- resolveProgram(command)Source
Resolve a program, using the program path and list of aliases.
Parameters
- command
stringThe name of the program
Returns
string| nil The absolute path to the program, ornilif it could not be found.
Usage
Locate the
helloprogram.shell.resolveProgram("hello") -- => rom/programs/fun/hello.lua
Changes
- New in version 1.2
- Changed in version 1.80pr1: Now searches for files with and without the
.luaextension.
- command
- programs([include_hidden])Source
Return a list of all programs on the path.
Parameters
- include_hidden?
booleanInclude hidden files. Namely, any which start with..
Returns
- {
string} A list of available programs.
Usage
Changes
- New in version 1.2
- include_hidden?
- complete(sLine)Source
Complete a shell command line.
This accepts an incomplete command, and completes the program name or arguments. For instance,
lwill be completed tols, andls rowill be completed tols rom/.Completion handlers for your program may be registered with
shell.setCompletionFunction.Parameters
- sLine
stringThe input to complete.
Returns
- {
string} | nil The list of possible completions.
See also
_G.readFor more information about completion.shell.completeProgramshell.setCompletionFunctionshell.getCompletionInfo
Changes
- New in version 1.74
- sLine
- completeProgram(program)Source
Complete the name of a program.
Parameters
- program
stringThe name of a program to complete.
Returns
- {
string} A list of possible completions.
See also
- program
- setCompletionFunction(program, complete)Source
Set the completion function for a program. When the program is entered on the command line, this program will be called to provide auto-complete information.
The completion function accepts four arguments:
- The current shell. As completion functions are inherited, this is not guaranteed to be the shell you registered this function in.
- The index of the argument currently being completed.
- The current argument. This may be the empty string.
- A list of the previous arguments.
For instance, when completing
pastebin put rom/stour pastebin completion function will receive the shell API, an index of 2,rom/stas the current argument, and a "previous" table of{ "put" }. This function may then wish to return a table containingartup.lua, indicating the entire command should be completed topastebin put rom/startup.lua.You completion entries may also be followed by a space, if you wish to indicate another argument is expected.
Parameters
- program
stringThe path to the program. This should be an absolute path without the leading/. - complete function(shell:
table, index:number, argument:string, previous: {string}):{string} | nil The completion function.
See also
cc.shell.completionVarious utilities to help with writing completion functions.shell.complete_G.readFor more information about completion.
Changes
- New in version 1.74
- getCompletionInfo()Source
Get a table containing all completion functions.
This should only be needed when building custom shells. Use
setCompletionFunctionto add a completion function.Returns
- { [
string] = { fnComplete =function} } A table mapping the absolute path of programs, to their completion functions.
- { [
- getRunningProgram()Source
Returns the path to the currently running program.
Returns
stringThe absolute path to the running program.
Changes
- New in version 1.3
- setAlias(command, program)Source
Add an alias for a program.
Parameters
Usage
Alias
vimto theeditprogramshell.setAlias("vim", "edit")
Changes
- New in version 1.2
- clearAlias(command)Source
Remove an alias.
Parameters
- command
stringThe alias name to remove.
- command
- aliases()Source
Get the current aliases for this shell.
Aliases are used to allow multiple commands to refer to a single program. For instance, the
listprogram is aliased todirorls. Runningls,dirorlistin the shell will all run thelistprogram.Returns
- { [
string] =string} A table, where the keys are the names of aliases, and the values are the path to the program.
See also
shell.setAliasshell.resolveProgramThis uses aliases when resolving a program name to an absolute path.
- { [
- openTab(...)Source
Open a new
multishelltab running a command.This behaves similarly to
shell.run, but instead returns the process index.This function is only available if the
multishellAPI is.Parameters
- ...
stringThe command line to run.
Usage
Launch the Lua interpreter and switch to it.
local id = shell.openTab("lua") shell.switchTab(id)
See also
Changes
- New in version 1.6
- ...
- switchTab(id)Source
Switch to the
multishelltab with the given index.Parameters
- id
numberThe tab to switch to.
See also
Changes
- New in version 1.6
- id