03 - definition of variables

1, Definition of variables

1. What are the variables?

In one sentence: variables are used to temporarily save data, which can be changed.

2. When do I need to define variables?

If a content needs to be used multiple times and appears repeatedly in the code, the content can be represented by variables. In this way, when modifying the content, you only need to modify the value of the variable. In the process of code operation, the execution results of some commands may be saved. If subsequent codes need to use these results, they can directly use this variable.

3. How to define variables?

Variable name = variable value
Variable name: used to temporarily save data
Variable value: temporary variable data

4. Definition rules of variables

Serial numberrule
1Case sensitive
2Variable names cannot have special symbols
3Variable names cannot start with numbers
4There must be no spaces on either side of the equal sign
5Try to know the meaning of variable names

5. What are the definition methods of variables?

1) Basic mode
Assign directly to a variable

[root@MissHou ~]# A=1234567 [root@MissHou ~]# echo $A 1234567
[root@MissHou ~]# echo ${A:2:4}

The similarities and differences between $variable name and ${variable name} are the same: variables can be called
Differences: ${variable name} can intercept only part of the variable, but $variable name cannot

[root@localhost add]# B=`date +%F`
[root@localhost add]# echo $B
[root@localhost add]# C=$(uname -r)
[root@localhost add]# echo $C

2, Interactive definition variable (read)

Purpose: let users assign values to variables themselves, which is more flexible.
Syntax: read [options] variable name
Common options:

-pDefine the information to prompt the user
-nDefine the number of characters (limit the length of variable values)
-sDo not display (do not display user input)
-tDefine the timeout time, and the default unit is seconds (limit the timeout time of user input variable value)

Usage 1: user defined variable value

[root@MissHou ~]# read name
[root@MissHou ~]# echo $name 
[root@MissHou ~]# read -p "Input your name:" name 
Input your name:tom
[root@MissHou ~]# echo $name 

Usage 2: the variable value comes from the file

[root@MissHou ~]# cat 1.txt
[root@MissHou ~]# read ip mask < 1.txt 
[root@MissHou ~]# echo $ip
[root@MissHou ~]# echo $mask

3, Define variable with type (declare)

Purpose: make some restrictions on variables and fix the type of variables, such as integer and read-only
Usage: declare option variable name = variable value
Common options:

[root@MissHou ~]# declare -i A=123 
[root@MissHou ~]# echo $A
[root@MissHou ~]# A=hello 
[root@MissHou ~]# echo $A 0

[root@MissHou ~]# declare -r B=hello
 [root@MissHou ~]# echo $B
[root@MissHou ~]# B=world
-bash: B: readonly variable 
[root@MissHou ~]# unset B
-bash: unset: B: cannot unset: readonly variable

4, Classification of variables

1. Local variable

Local variable: a variable defined by the current user. The current process is valid. Other processes and child processes of the current process are invalid.

2. Environmental variables

Environment variable: the current process is valid and can be called by child processes.

  • env view the environment variables of the current user
  • set queries all variables (temporary variables and environment variables) of the current user
  • export variable name = variable value or variable name = variable value; export variable name
[root@MissHou ~]# export A=hello 	 Temporarily changes a local variable (temporary variable) into an environment variable
[root@MissHou ~]# env|grep ^A A=hello

Permanent: vim /etc/profile
Or ~ / bashrc export A=hello
Or A=hello export A

Note: there is a variable PATH and environment variable in the system
export PATH=/usr/local/mysql/bin:$PATH

3. Global variable

  • Global variable: all users and programs in the world can call and inherit, and new users can call by default
  • Interpretation of relevant configuration files
    Note: after the above files are modified, you need to re source to make them effective or log out and log in again.

The order in which users log in to the system to read relevant files

  1. /etc/profile
  2. $HOME/.bash_profile
  3. $HOME/.bashrc
  4. /etc/bashrc
  5. $HOME/.bash_logout

4. System variable

Built in variablemeaning
$?The status returned after the execution of the previous command; A status value of 0 indicates normal execution, and a non-0 indicates abnormal execution or error
$0The name of the program or script currently executing
$#The number of parameters followed by the script
$*All parameters behind the script are output as a whole, and each variable parameter is separated by a space
$@All parameters behind the script are independent and output
$#1~$9For the location parameter after the script, $1 represents the first location parameter, and so on
${10} ~ ${n}Expand the location parameter. The 10th location variable must be enclosed in {} braces (expanded with more than 2 digits)
$$The process number of the current process, such as echo$$
$!The last process number running in the background (current terminal)
!$Call the parameters in the last command history

5, Simple four arithmetic

Arithmetic operations: by default, the shell can only support simple integer operations
Operation contents: addition (+), subtraction (-), multiplication (*), division (/), remainder (%)
1. The four operations are consistent

expressiongive an example
$(( ))echo $((1+1))
$[ ]echo $[10-5]
exprexpr 10 / 5
letn=1;let n+=1 is equivalent to let n=n+1

6, Array

1. Definition of array

  • Normal array: only integers can be used as array indexes (subscripts of elements)
  • Associative array: you can use strings as array indexes (subscripts of elements)

2. Normal array definition

  • One at a time

Array name [index subscript] = value

  • Assign multiple values at a time

Array name = (value 1, value 2, value 3...)
array=(var1 var2 var3 var4)

Array1 = (` cat / etc / passwd `) ------------------ assign each line in the file to array1 array
array2=(`ls /root`)
array3=(harry amy jack "Miss Hou")
array4=(1 2 3 4 "hello world" [10]=linux)

3. Array reading

${array name [element subscript]}

echo ${array[0]} ----- get the first element in the array
echo ${array []} ---- get all elements in the array
echo ${#array []} ------ get the number of all elements in the array
echo ${!array [@]} ------- get the index subscript of the array element
echo ${array[@]:1:2} ------ access the specified element; 1 means to obtain from the element with subscript 1; 2 means to get the following elements

View common array information:

  [root@MissHou ~]# declare -a

4. Associative array definition

1) First declare the associative array

declare -A asso_array1
declare -A asso_array2
declare -A asso_array3

2) array assignment

  • Assign one value at a time

Array name [index or subscript] = variable value
asso_array1 [linux]=one
asso_array1 [java]=two
asso_array1 [php]=three

  • Assign multiple values at a time
 asso_array2=([name1]=harry [name2]=jack [name3]=amy [name4]="Miss Hou")
  • View associative arrays
#declare -A
declare -A asso_array1='([php]="three" [java]="two" [linux]="one" )'
declare -A asso_array2='([name3]="amy" [name2]="jack" [name1]="harry" [name4]="Miss Hou" )'
  • Get associative array value
# echo ${asso_array1[linux]} 
# echo ${asso_array1[php]} 
# echo ${asso_array1[*]} 
three two one
# echo ${!asso_array1[*]}
php java linux
# echo ${#asso_array1[*]}
# echo ${#asso_array2[*]}
# echo ${!asso_array2[*]}
name3 name2 name1 name4

Keywords: shell

Added by WeddingLink on Fri, 31 Dec 2021 15:05:18 +0200