typeset Command in Linux



The typeset command in Linux declares and modifies the variables. It is a built-in command that provides more control over variable attributes. It is more commonly used in ksh and zsh for its advanced features. In bash, typeset is an alias to declare, so they behave the same.

Table of Contents

Here is a comprehensive guide to the options available with the typeset command −

Syntax of typeset Command

The syntax of the typeset command in Linux is as follows −

typeset [options] name[=value]

In the above syntax, the [options] field is used to specify the optional flags that modify how the variable behaves. For example, integer, read-only, and others. The parameter name is used to specify the name of the variable or function being declared or modified. An optional "=value" can be provided to initialize the variable upon declaration.

typeset Command Options

The options for the Linux typeset command are listed below −

Option Description
-C Compound variable. Copies the value if it is a compound variable; otherwise, unsets the existing variable before assignment.
-D Reserved for future use.
-H Maps UNIX to hostname file on non-UNIX machines.
-f Converts uppercase characters to lowercase. Turns off -u. Function definitions persist if -x is set.
-i Declares the variable as an integer. Sets arithmetic base if a non-zero value is provided.
-l Converts uppercase characters to lowercase. Turns off -u.
-m Moves the value of one variable to another and unsets the original. Cannot be combined with other options.
-r Marks the variable as read-only. Prevents reassignment.
-t Tags variables. Tags are user-defined and have no special shell meaning.
-u Converts lowercase characters to uppercase. Turns off -l.
-x Exports variables for access in sub-processes.
-L[n] Left-justifies value by removing leading blanks (and zeros if -Z is set), then pads or truncates at the end. Disables -R.
-R[n] Right-justifies value by adding leading blanks or truncating the start. Disables -L.
-Z[n] Right-justifies numeric values using leading zeros if the first non-blank character is a digit. Disables -L.

Examples of typeset Command in Linux

This section explores how to use the typeset command in Linux with examples −

  • Declaring a Variable
  • Declaring an Integer Variable
  • Declaring a Read-Only Variable
  • Converting to Uppercase
  • Converting to Lowercase
  • Right Justifying and Padding
  • Right Justifying with Zeros
  • Left Justifying and Padding

Declaring a Variable

To declare a variable, use the typeset command followed by the variable name and its value −

typeset name="tutorialspoint"

To display the variable, use the echo command −

echo $name
typeset Command in Linux1

Declaring an Integer Variable

To declare an integer variable using the typeset command, use the -i option −

typeset -i num=100
num+=1
typeset Command in Linux2

Declaring a Read-Only Variable

A read-only variable is a variable that cannot be modified or reassigned after its initial declaration. To declare a read-only variable, use the -r option.

typeset -r name="tutorialspoint"
typeset Command in Linux3

Converting to Uppercase

To convert a string value of a declared variable to uppercase, use the -u option −

typeset -u name="tutorialspoint"
typeset Command in Linux4

The above command automatically converts assigned values to uppercase.

Converting to Lowercase

To convert a string value of a declared variable to lowercase, use the -l option −

typeset -l name="TUTORIALSPOINT"
typeset Command in Linux5

Right Justifying and Padding

To right-justify and pad, use the -R option −

typeset -R10 right="cat"

To verify, use the echo command in the following way −

echo ">$right<"
typeset Command in Linux6

Right Justifying with Zeros

To right-justify with zeros, use the -Z option −

typeset -Z5 code=44

To verify, use the echo command −

echo "$code"
typeset Command in Linux7

Left Justifying and Padding

To left-justify and pad, use the -L option −

typeset -L10 left="dog"

To verify, use the echo command in the following way −

echo ">$left<"
typeset Command in Linux8

Note − Bash does not support the -L, -R, or -Z options with typeset. These flags are specific to KornShell (ksh) or Zsh, not Bash.

Conclusion

The typeset command in Linux is a built-in shell feature used to declare and manage variables with specific attributes. It provides more control over variable behavior, such as setting them as integers, read-only, or changing text cases automatically.

While typeset behaves the same as declare in Bash, it is more advanced and commonly used in shells like ksh and zsh.

Advertisements