Shell notes: variables

Usually speaking of variables, it may refer to user-defined variables, but the concept of variables in Shell includes environment variables, system pre-defined variables, positional parameter variables, and numeric variables in addition to user-defined variables.

 

User-defined variables
Note that user-defined variables only take effect in the current shell terminal.
Custom variables: Variable names can be composed of letters, numbers, and underscores, but they cannot begin with a number.
Variable types: In Bash, the default types of variables are all string types. If you want to perform numeric operations, you must specify the variable type as a numeric type. See the "Numerical Operations" content below.
Variable assignment: Use equal sign = connection between the variable and the value, and there must be no spaces on both sides of the equal sign.
Command output is assigned to a variable: use the format of "variable name = $ (command)".
Reference variable: "$ variable name", such as "$ name".
Variable superposition: such as "aa = 123", there are two ways, one is to use double quotes and the $ character, "aa =" $ aa "456", and the other is the $ character and curly brackets, "aa = $ { aa} 456 ".
set: You can view all variables in the system.
unset variable name: delete a variable.

 

Environment variables
Environment variables are divided into two parts, one is the system environment variables, and the other is the user-defined environment variables (recommended to use all capital naming).
Environment variables are valid in the current shell environment and its subshell environments, and environment variables can be written to the configuration file so that they can take effect in all shell environments.
export variable name [= value]: declare or set an environment variable.
env: View all environment variables (set is to view all variables, including local variables).
unset variable name: delete a variable.
Common system environment variables:

  • PATH: The path of the system search command.

 

System pre-defined variables
Pre-defined is actually defined by the system for you, you can use it directly, so the system pre-defined variables are some fixed variables, you can directly use them when needed.
$ ?: indicates the return status of the last command executed. If the value of this variable is 0, it means that the last command was executed correctly; if the value of this variable is non-zero (the specific number returned is defined by the command itself), it means that the last command was not executed correctly.
$$: The process number (PID) of the current process.
$ !: The process number (PID) of the last process running in the background.

 

Position parameter variables
Position parameter variables represent the parameters passed to the script when the script is run. The position parameter variables are actually part of the system's predefined variables, but because they are related to "position", they are taken out separately.
$ [n]: $ 0 represents the command itself or the script file itself, $ 1- $ 9 represents the first to ninth parameters, and the parameters of 10 and above need to use braces, such as $ {10}.
$ *: Represents all the parameters in the command line, and it is a whole, that is, it will only loop once during the for loop, and output all parameters at once.
$ @: Represents all the parameters in the command line, but it is equivalent to a sequence, that is, the for loop will output one parameter each time.
$ #: Represents the number of parameters on the command line.

 

Receive keyboard input
read [option] [variable name]: accept keyboard input and assign the input value to the specified variable.
Options:

  • -p "prompt message": output the prompt message when waiting for the read input.
  • -t seconds: The read command will always wait for user input. Use this option to specify the waiting time.
  • -n Number of characters: The read command only accepts the specified number of characters. If the number of characters is enough, the command will be executed automatically.
  • -s: hide the input data, suitable for the input of confidential information (no need to press Enter at this time, as long as the number of characters is enough, it will be automatically executed).

 

Numerical operations
The variables in Linux are all strings by default. To perform numerical operations, you need to declare the variable type for the variable.
declare [+/-] [options] Variable name: declare the variable.
Options:

  • -: Set type attributes for variables.
  • +: Cancel the type attribute of the variable.
  • -i: Declare the variable as an integer.
  • -x: declare variables as environment variables (export can also declare variables as variables, and export is more commonly used).
  • -p: Display the type of the specified variable being declared.

Examples:

aa = 11 
bb = 22 #Method 
1: Declare the variable cc as an integer 
declare -i cc = $ aa + $ bb
 echo $ cc #Method 
2: Use the expr command, note that the spaces on both sides of the plus sign cannot be omitted 
dd = $ ( expr $ aa + $ bb)
 echo $ dd #Method 
three: $ ((expression)) or $ [expression], the former is more commonly used, and method three is recommended. In the expression, you can use parentheses to adjust the priority as in mathematics 
ee = $ (($ aa + $ bb))
 echo $ ee

 

Guess you like

Origin www.cnblogs.com/guyuyun/p/12735039.html