LinuxCommandLibrary

typeset

Define variable attributes and data type

TLDR

View documentation for the original command

$ tldr declare
copy

SYNOPSIS

typeset [-afFirtux] [-p] name[=value] ...

PARAMETERS

-a
    Declare name as an indexed array. If the -a option is not used, arguments to the variable name will create a normal variable.

-f
    Display function definitions; no assignments are performed. Used without other options, lists function names. With '-f' and 'name', lists the definition of function 'name'.

-F
    Display function names only, without definitions.

-i
    Declare name as an integer. Arithmetic evaluation is performed when value is assigned to name. An argument of zero is returned when value is evaluated to zero.

-p
    Display the attributes and values of each name. If no names are given, display the attributes and values of all variables. Useful for debugging and understanding the current state of variables.

-r
    Declare name as read-only. Attempts to assign a value to a read-only variable will result in an error.

-t
    Apply the 'trace' attribute to name. Traced functions inherit the DEBUG and RETURN traps of the calling function. The trace attribute has no special meaning for variables.

-u
    When assigning a value to name, automatically convert it to uppercase.

-x
    Mark name for export to subsequent commands via the environment. It makes the variable available to child processes. Same as 'export'.

DESCRIPTION

The typeset command in Linux, primarily used in the bash shell, is a powerful built-in command for declaring variables and setting their attributes. It allows you to control aspects of how variables are stored and treated, such as their case (uppercase, lowercase), type (integer, floating-point, read-only), alignment, and more.
Essentially, typeset provides fine-grained control over variable behavior, enabling you to create robust and well-defined shell scripts.
It's a more flexible and feature-rich alternative to simple variable assignments, allowing for improved code readability and error prevention. Understanding typeset is crucial for writing advanced shell scripts where variable attributes are important for achieving specific functionality and maintainability.

CAVEATS

The behavior of typeset can vary slightly between different shell implementations. It is primarily used in bash, ksh, and zsh. The effects are typically local to the current shell session, unless variables are exported using the '-x' option.

PRACTICAL USAGE

Common uses include declaring integer variables for arithmetic operations (typeset -i), making variables read-only to prevent accidental modification (typeset -r), and exporting variables to the environment for use by child processes (typeset -x).
It is often used in scripts where you need to ensure variables maintain a certain format or type throughout their lifetime. For example, to define an integer variable: typeset -i counter=0
To define a read-only variable: typeset -r VERSION="1.0"

HISTORY

The typeset command originates from the Korn shell (ksh) and was subsequently adopted by other shells such as bash and zsh. It was designed to provide more explicit control over variable attributes than simply assigning values. Over time, it has become an integral part of shell scripting, allowing developers to write more robust and maintainable code by defining variable types and behaviors explicitly.

SEE ALSO

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

Copied to clipboard