The declare and typeset commands are the built-in commands for Bash, both of which are exactly the same, used to declare shell variables and to set the properties of the variables.
The Declare command (alias typeset) is a shell built-in command for declaring shell variables and setting variable properties, or viewing defined shell variables and functions. If you do not add any parameters, all the shell variables and functions are displayed (the same as the effect of the set Directive).
Common Command Parameters
-r Set Variable to read-only
-I set Variable to integer
-A sets the variable to an array of arrays
-F If there are no arguments, then all the functions of the previous script definition are listed, and if there are any parameters, the function named by the parameter is listed.
The-X setting variable can also be accessed from outside the script
-A: Declare an array variable-A: Declare an associative array, you can use a string as an array index-f: Show only defined Functions-f: Do not show function definition-i: Declare integer variable-L: Change the value of the variable to lowercase letters- U: Uppercase of variable value-r: Set read-only property-T: Set Variable tracking property for tracking function debugging, no special meaning for variable-x: Change the specified shell variable to environment variable-p: Shows the way and value of the variable definition + : The variable properties are canceled, but +a and +r are not valid to delete the array and read-only properties, you can use unset to delete the array, but unset cannot delete the read-only variable
(1) Define an associative array and access it.
declare- A assarray= ([lucy]=beijing [Yoona]=shanghai) #读取关联数组全部内容 echo ${assarray[*]} #或 echo Span class= "hljs-variable" >${assarray[@]} #输出beijing Shanghai #读取指定索引的数组值 echo ${assArray[ Lucy]} #输出: Beijing #列出数组索引列表 echo ${!assarray[*]} #或 echo Span class= "hljs-variable" >${!assarray[@]} #输出yoona Lucy
(2) defining read-only variables
declare -r name1="lvlv1"#或typeset -r name2="lvlv2"#或readonly name3="lvlv3"
The shell specifies that the read-only variable life cycle is the same as the current shell script process, and cannot eliminate read-only properties and delete read-only variables unless the current shell script process is kill.
(3) Use -p
options to show how variables name1 and name2 are defined and current values.
declare -p name1 name2 name3#输出declare -r name1="lvlv1"declare -r name2="lvlv2"declare -r name3="lvlv3"
(4) Use the -x
option to convert the shell variable to a temporary environment variable for use by other shell processes of the current shell session, and to exit the current shell session is invalidated.
declare -x name1;
(5) Displays all shell environment variables.
declare -x
(6) Use +x
the option to cancel the variable as an environment variable.
delcare +x name1
(7) Declare the integer variable, the value of the assigned floating-point type will be an error.
declare -i integer=666
Format: declare-p var
Format: typeset-p var
Displays the value of the specified variable var.
Format: Declare var=value
Format: typeset var=value
Format: Var=value
Declares a variable and assigns a value.
Format: declare-i var
Format: typeset-i var
Variable var is defined as an integer. After that, you can evaluate the expression directly, and the result can only be integers. If the evaluation fails or is not a whole number, it is set to 0.
Format: declare-r var
Format: typeset-r var
Format: readonly var
Declare the variable var as a read-only variable. Read-only variables are not allowed to be modified and are not allowed to be deleted.
Format: declare-a var
Format: typeset-a var
Declare the variable var as an array variable. But that's not necessary. All variables do not have to be explicitly defined to be used as arrays. In fact, in a sense, all variables seem to be arrays, and assigning to a variable with no subscript is the same as assigning a value to "[0]".
Format: declare-f
Format: typeset-f
Displays all the custom functions, including the name and function body.
Format: declare-f
Format: typeset-f
Displays all custom function names.
Format: declare-f func
Format: typeset-f func
Displays only the function definition for the specified function func.
Format: Declare-x var
Format: typeset-x var
Format: Export var
Variable var is set to an environment variable so that it can be used in subsequent scripts and programs.
Format: Declare-x var=value
Format: Typeset-x var=value
Format: Export Var=value
The variable var is set to the environment variable and assigned value.
Linux under Bash variable use, echo,read,declare,typeset,array,local variable set rule 1, variable and variable content with an equal sign to connect
myname=del1214
2, the equal sign can not be directly connected to the space, the following assignment is wrong
MyName = del1214 or Myname= del 1214
3, the variable name can only be English letters and numbers, but the first character can not make a number, the following is the wrong
1myname=del1214
4, variable content has a space can be used double quotation marks "or single quotation marks" to connect the content, but
The special characters within the double quotation mark $, can retain the original characteristics, as follows:
var= "Lang is $LANG" echo $var show Lang is en_US
The special characters in single quotation marks are general characters, as follows:
Var= ' Lang is $LANG ' the echo $var shows Lang is $LANG
5, use the escape character to the special symbol (carriage return, $,\, space, ' etc) into the general character 6, use the command in the variable, need to use ' command ' or $ (command), for example to obtain the kernel version number
version=$ (UNAME-R)
Echo $version
2.6.xxxxxxxxx
7, add the contents of the variable (string addition), using "$ variable name" or ${variable}, for example:
Path= "$PATH":/home/bin
8. Convert variables to environment variables, use Export command
Export PATH
9, the environment variable is generally all uppercase, the custom variable uses the lowercase letter 10, cancels the variable command unset,unset variable name variable by the keyboard input read
To read a variable from a keyboard input, use the Read command
Read [-pt] variable name
-P: prompt followed by character
-T: Number of seconds to wait after
For example:
Read-p "Please type your name"-T ten Yourname
Definition of variable type Declare/typesetdeclare
declare [-AIXR] variable name
-A: Defines the following variable name as array array type
-I: Defines the following variable names as integer digits
-X: Defines the following variable name as an environment variable, which functions with the export
+X: Canceling environment variables
-r: Define variable name as ReadOnly class ing, cannot change cannot unset
Once the read-only property is determined, the type of the variable can only be restored by logging off and then landing
Bash's basic definition of variable type
The variable type defaults to a string, and an ambiguous specified type is a string, 1+2 is not automatically counted as 3
A numeric operation in bash can only be an integer, 1/3 result is 0
Typeset
typeset [-aaffgilrtux] [-p] name[=value]
The explanation is not very whole, still use declare bar
The use of array arrays in variables is set by:
Var[index]=content
For example:
Var[1]= "AAA"
Var[2]= "BBB"
var[3]= "CCC"
echo "${var[1]},${var[2]},${var[3]}"
Aaa,bbb,ccc
Deletion and substitution of variable contents
Variable Setting method
|
Description |
${variable # keyword} ${variable # #关键词} |
The most "short" data will be deleted if the variable content starts with the word "header" and the data matches the keyword. The most "long" data will be deleted if the variable content starts with the "header" and the data matches the keyword. |
${Variable% keyword} ${variable percent percent keyword} |
The most "short" data will be deleted if the variable content "forwards" data matches the keyword The most "long" data will be deleted if the variable content "forwards" data matches the keyword |
${variable/old string/new string} ${variable//old string/new string} |
If the contents of the variable conform to the old string, the "first" old string is replaced by the new string If the contents of the variable conform to the old string, the "all" old string is replaced by the new string |
Judgment and substitution of variable contents
Variable Setting method |
STR is not set to null |
Str is an empty string "" |
STR is set to a non-empty string |
VAR=${STR-EXPR} VAR=${STR:-EXPR} |
var=expr var=expr |
Var= var=expr |
Var= $str Var= $str |
VAR=${STR+EXPR} |
Var= |
var=expr |
var=expr |
VAR=${STR:+EXPR} |
Var= |
Var= |
var=expr |
VAR=${STR=EXPR}
VAR=${STR:=EXPR} |
str=expr var=expr
str=expr var=expr |
STR does not change Var=
str=expr var=expr |
STR does not change Var= $str
STR does not change Var= $str |
VAR=${STR?EXPR} |
Expr output to stderr |
Var= |
Var= $str |
VAR=${STR:?EXPR} |
Expr output to stderr |
Expr output to stderr |
Var= $str |
My personal understanding is that
-: When a variable does not contain the following string
+: When a variable contains the following string
=: variable equals subsequent string
?: Output
: Colon: variable is an empty string when the opposite action is taken without a colon
Linux command (6/28)--declare/typeset command