Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

typeset - set attributes for variables

&pagelevel(4)&pagelevel

The typeset command can be used for two purposes:

  • to list variable names and other optional information on standard output

  • to set attributes and values for shell variables.


Syntax


typeset[ option][ name[=value]]...

If neither option nor name[=value] is given, the names and attributes of all variables are listed on standard output.

If there are options but no name arguments, all variables which have these options set are listed along with their attributes.

Using a plus sign instead of a minus sign stops the values from being printed.

option can be used to assign one or more attributes to the variable name[=value]. When typeset is invoked inside a function, a new instance of the variable name is created. The value and its attribute are restored when the function completes.

Using a minus sign before the option letters turns on the corresponding attributes; a plus sign turns them off.

You may enter the following list of attributes as options:

-L[num]

Left-justifies value and removes leading blanks.

If num is non-zero, it defines the length of the field; otherwise, the field length is determined by the length of the first value assignment.

This length is used for subsequent assignments. When the variable is reassigned, it is padded to the right with blanks or truncated as necessary.

Leading zeros are removed if the -Z option is also set.

The -L option turns off the -R option.

-R[num]

Right-justifies value and pads it with leading blanks.

If num is non-zero, it defines the length of the field; otherwise, the field length is determined by the length of the first value assignment. This length is used for subsequent assignments.

When the variable is reassigned, the field is either padded to the left with blanks or truncated from the end.

The -R option turn off the -L option.

-Z[num]

Right-justifies the value and pads it with leading zeros if the first non-blank character is a digit and the -L option has not been set. If num is non-zero, it defines the length of the field; otherwise, the field length is determined by the length of the first value assignment.

-f

The names refer to function names rather than variable names. The POSIX shell stores the functions in the .sh_history file. Consequently, you cannot display a function definition on the screen if .sh_history does not exist or if the nolog option was not set when the function was read. No assignments can be made, and the only other valid options in combination with -f are:

-t

Turns on execution tracing for the function.

-u

Causes the function to be marked undefined. The FPATH variable is searched to find the function definition the next time the function is referenced.

-x

Allows the function definition to remain in effect across POSIX shell scripts invoked by name. In other words, the function is exported.

-i[num]

The variable is an integer. This option makes arithmetic faster. If num is non-zero, it defines the output arithmetic base; otherwise, the first assignment determines the output base.

-l

All uppercase characters are converted to lowercase. The -u option is turned off.

-r

The given variables are marked readonly, which means that their values cannot be changed by subsequent assignment.

-t

Tags the variables. Tags are user-definable and have no special meaning to the POSIX shell.

-u

All lowercase characters are converted to uppercase characters. The -l option is turned off.

-x

The given names are marked for automatic export to any new environment.

Error

sh: variable: bad number

if the number assigned to the variable in the -i option was not an integer.

Example

$ typeset -L var1="  31"
$ typeset -L
...
var1='31  '
...
$ typeset -l var2=VALUE2
$ typeset -l
var2=value2

See also

readonly, export, set, env