Linux variables are roughly divided into four categories: User-defined variables, environment variables, positional parameter variables, predefined variables four categories, the most commonly used is the first three categories, where positional parameter variables can also be called script variables, often used in the script
a Linux variable classification
1. User-defined variables: Users can perform crud operations, the scope of the current shell, defined in the script can only be used in the script
2. Environment variables: The main preservation is related to the system operating environment variables, you can add and modify
3. Position parameter variable: mainly in the script is called, accept the parameters passed by the script, so it can be called script variables
4. Predefined variables: Some variables of the system's prefabrication
Two variables Considerations
1. Variable nouns cannot begin with numbers,
2. The type of the variable is a string type, you need to use $ ((1+2) for numeric operations)
3. Variable definition = must not have spaces on both sides
4. Variable values can use special symbols, escape characters
5. The value of the variable can be the result of the command, with an inverted quote or $ () containing the command
6. Environment variable name recommended ALL CAPS
three user-defined variables
User-defined variables, if used in the shell window, are valid only for the current shell and not for their child shells.
1. Definition: name=value
2. Reference: $name
3. Delete: unset name
4. View: set, will list all the variables of information, including user-defined variables, environment variables, predefined variables
5. Usage examples:
Four environment Variables
If the environment variable is set in the Shell window, it is valid for the current shell and its child shell, and if it is written to the configuration file, it will take effect for all shells. Environment variables are typically used to store system-related commands.
1. Definition 1: export name=value, define new environment variables
definition 2: export name, convert user custom variable to environment variable
2. Reference: $name
3. Delete: unset name
4. View: env
5. Usage examples:
Five predefined variables
1. Examples of predefined variables:
2. Usage examples
six position parameter variables
1. Position variable:
2. Usage examples:
#!/bin/bash
#脚本中使用位置参数变量的时候, $# and "$#" are all OK, if you want to output $ #文件则需单引号
echo ' num $#: ' $# echo ' args
$*: ' $*
echo ' args $ @: ' $@ echo ' cmd $: ' $ Echo ' cmd $: ' $ ' $ Echo ' cmd $: '
${10}
Echo ' ============= = $* ======================== ' for
i in ' $* '
does
echo ' arg: ' $i
done
Echo ' ======== = = = = = = = = = = = = $@ ======================== ' for
y in ' $@ ' does
Echo ' arg: ' $y
done
3. Output Result: