Linux command
typeset 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Declare an integer variable
typeset -i [count]=0
Declare a read-only variable
typeset -r [CONSTANT]="[value]"
Declare an uppercase variable
typeset -u [name]="[value]"
Declare a lowercase variable
typeset -l [name]="[VALUE]"
Export a variable
typeset -x [PATH]="[/usr/bin]"
Declare an array
typeset -a [myarray]=([one] [two] [three])
Declare an associative array
typeset -A [mymap]=([key1]="[val1]" [key2]="[val2]")
List all variables
typeset -i
说明
typeset is a shell builtin that declares variables and assigns them attributes. In bash, it is a synonym for declare and is provided for compatibility with ksh (KornShell). Variables can be given type attributes like integer (-i) for automatic arithmetic evaluation, or transformation attributes like uppercase (-u) and lowercase (-l). The read-only attribute (-r) prevents modification after assignment. When used inside a function without -g, typeset creates a local variable that shadows any global variable of the same name. The variable is restored when the function exits. Without arguments, typeset displays all variables with their attributes. With attribute flags alone (e.g., typeset -i), it lists variables having that attribute.
参数
- -a
- Declare indexed array variable
- -A
- Declare associative array variable (bash 4+)
- -f
- Display function names and definitions
- -F
- Display function names only
- -g
- Create global variable when used in a function (bash only)
- -i
- Treat variable as integer; arithmetic evaluation on assignment
- -l
- Convert value to lowercase on assignment
- -n
- Create nameref (reference to another variable)
- -r
- Make variable read-only
- -t
- Give variable the trace attribute
- -u
- Convert value to uppercase on assignment
- -x
- Export variable to environment of child processes
- -p
- Display attributes and values of variables
FAQ
What is the typeset command used for?
typeset is a shell builtin that declares variables and assigns them attributes. In bash, it is a synonym for declare and is provided for compatibility with ksh (KornShell). Variables can be given type attributes like integer (-i) for automatic arithmetic evaluation, or transformation attributes like uppercase (-u) and lowercase (-l). The read-only attribute (-r) prevents modification after assignment. When used inside a function without -g, typeset creates a local variable that shadows any global variable of the same name. The variable is restored when the function exits. Without arguments, typeset displays all variables with their attributes. With attribute flags alone (e.g., typeset -i), it lists variables having that attribute.
How do I run a basic typeset example?
Run `typeset -i [count]=0` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -a do in typeset?
Declare indexed array variable