The typeset built-in prints or sets variables or functions.
Syntax
-
typeset [-gprxX] [variable[=value]…]
-
typeset -f[pr] [function…]
Description
If executed without the -f
(--functions
) option, the typeset built-in
prints or sets variables to the standard output.
Otherwise, it prints or sets functions.
If executed with the -p
(--print
) option, the built-in prints the
variables or functions specified by operands.
Without the option, it sets variables or functions.
If no operands are specified, it prints all existing variables or functions,
regardless of whether the -p
(--print
) option is specified.
Options
-
-f
-
--functions
-
Print or set functions rather than variables.
-
-g
-
--global
-
When setting a new variable, the variable will be a global variable if this option is specified. Without this option, the variable would be a local variable.
When printing variables, all existing variables including global variables are printed if this option is specified. Without this option, only local variables are printed.
-
-p
-
--print
-
Print variables or functions in a form that can be parsed and executed as commands that will restore the currently set variables or functions.
-
-r
-
--readonly
-
When setting variables or functions, make them read-only.
When printing variables or functions, print read-only variables or functions only.
-
-x
-
--export
-
When setting variables, mark them for export, so that they will be exported to external commands.
When printing variables, print exported variables only.
-
-X
-
--unexport
-
When setting variables, cancel exportation of the variables.
Operands
- variable (without value)
-
The name of a variable that is to be set or printed.
Without the
-p
(--print
) option, the variable is defined (if not yet defined) but its value is not set nor changed. Variables that are defined without values are treated as unset in parameter expansion. - variable=value
-
The name of a variable and its new value.
The value is assigned to the variable (regardless of the
-p
(--print
) option). - function
-
The name of an existing function that is to be set or printed.
Exit status
The exit status of the typeset built-in is zero unless there is any error.
Notes
A global variable cannot be newly defined if a local variable has already been
defined with the same name.
The local variable will be set regardless of the -g
(--global
) option.
The typeset built-in is an elective built-in. It cannot be used in the POSIXly-correct mode because POSIX does not define its behavior.
The export and readonly built-ins are
equivalent to the typeset built-in with the -gx
and -gr
options,
respectively.
The local built-in is equivalent to the typeset built-in
except that the -f
(--functions
) and -g
(--global
) options cannot be
used.