Skip to content

Terminal I/O

Read from stdin and write to stdout/stderr for CLI applications.

This module only works inside terminal context. You cannot use it from regular functions—only from processes running on a Terminal Host.
local io = require("io")

Write strings to stdout without newline:

local ok, err = io.write("text", "more")
ParameterTypeDescription
...anyVariable number of values to write (coerced to string)

Returns: boolean, error

Write values to stdout with tabs between and newline at end:

io.print("value1", "value2", 123)
ParameterTypeDescription
...anyVariable number of values to print

Returns: boolean, error

Write values to stderr with tabs between and newline at end:

io.eprint("Error:", message)
ParameterTypeDescription
...anyVariable number of values to print

Returns: boolean, error

Read up to n bytes from stdin:

local data, err = io.read(1024)
ParameterTypeDescription
nintegerNumber of bytes to read (default: 1024, values <= 0 become 1024)

Returns: string, error

Read a line from stdin up to newline:

local line, err = io.readline()

Returns: string, error

Enable or disable raw terminal mode (disables line buffering and echo):

local ok, err = io.raw(true) -- enable
local ok, err = io.raw(false) -- disable
ParameterTypeDescription
enablebooleantrue to enable, false to disable (default: true)

Returns: boolean, error

Raw mode is reference-counted — each io.raw(true) must be matched by an io.raw(false). The terminal resets to normal mode automatically on process exit.

Flush stdout buffer:

local ok, err = io.flush()

Returns: boolean, error

Get command line arguments:

local args = io.args()

Returns: string[]

ConditionKindRetryable
No terminal contexterrors.UNAVAILABLEno
Write operation failederrors.INTERNALno
Read operation failederrors.INTERNALno
Flush operation failederrors.INTERNALno

See Error Handling for working with errors.