LinuxCommandLibrary

typeset

Define variable attributes and data type

TLDR

View documentation for the original command

$ tldr declare
copy

SYNOPSIS

typeset [options] [name[=value] ...]
typeset -f [options] [name ...]

PARAMETERS

-a
    Declare an indexed array variable.

-A
    Declare an associative array variable (Bash 4+).

-f
    Operate on function names. When used alone, it lists function definitions. With arguments, it sets attributes for those functions.

-g
    Create global variables when used within a shell function (Bash only).

-i
    Treat the variable as an integer. Arithmetic evaluation is performed during assignment.

-l
    Convert all characters to lowercase upon assignment to the variable.

-n
    Create a nameref, a reference to another variable (Bash 4.3+, ksh93).

-p
    Display attributes and values in a reusable format suitable for re-evaluation by the shell.

-r
    Mark variables as read-only. Their values cannot be changed after initial assignment.

-t
    Set the 'trace' attribute for functions (Bash debugging). In ksh, it sets a 'tag' attribute for variables.

-u
    Convert all characters to uppercase upon assignment to the variable.

-x
    Mark variables for export to the environment of child processes.

DESCRIPTION

typeset is a powerful shell built-in command, primarily found in KornShell (ksh) and as an alias for declare in Bash. It allows users to declare shell variables and functions, assign values, and set various attributes for them. These attributes can control aspects like variable scope, integer properties, read-only status, exportability, and case conversion. For instance, it can declare an integer variable, an array, or export a variable to the environment. When used without options, typeset lists the names and attributes of all shell variables and functions. It is a fundamental tool for managing variables and functions within shell scripts and interactive shell sessions, offering more granular control than simple assignment.

CAVEATS

typeset is a shell built-in command, not an external executable. Its behavior can vary slightly between shells (e.g., bash, ksh, zsh).
In Bash, typeset is exactly equivalent to declare. Using declare is generally preferred for clarity and consistency in Bash scripts.
The meaning of the -t option differs between ksh (tag variable) and bash (trace function).

BUILT-IN STATUS

typeset is a shell built-in command, meaning it is executed directly by the shell and not as an external program. This makes it faster and allows it to directly manipulate the shell's internal state, such as environment variables and function definitions.

VARIABLE SCOPING (BASH SPECIFIC)

In Bash, declare (and thus typeset) used without options inside a function typically declares a local variable by default, overriding a global variable of the same name. To explicitly declare or modify a global variable from within a function in Bash, the -g option must be used.

HISTORY

The typeset command originated in the KornShell (ksh) as a powerful mechanism for defining variable attributes and types, offering more robust variable management capabilities beyond simple string assignments. When Bash was developed, it adopted a similar functionality, but named its built-in declare. For compatibility with ksh scripts, Bash also includes typeset as an alias that behaves identically to declare. Its usage is deeply rooted in shell scripting for controlling variable behavior.

SEE ALSO

declare(1), export(1), readonly(1), local(1)

Copied to clipboard