By default, bash shell is a non-typed footfall language. Unless specifically declared with declare, it does not distinguish whether a variable is an integer variable, a floating point variable, or a string variable. All variables in bash shell are considered as strings and do not need to be declared during use.
1. variable naming
The naming rules for variables in bash shell are the same as those in C. They must consist of letters, numbers, and underscores. The first character must be a letter or underscore. There is no limit on the variable length, however, English letters are case sensitive. Although you do not need to declare variables when using them in bash shell, it is recommended to declare and add comments to some important variables for reading and maintenance. After a variable is declared or created, the scope of the variable is the current shell. The sub-shell cannot obtain the variables defined in the parent shell unless the environment variable is used for the variable.
2. Set Variables
In bash shell, it is easy to set the value of a variable by following the following steps:
Variable name = Value
To change the value of a variable. Note thatNo space is allowed on either side of the equal signIf the value contains spaces, it must be enclosed in quotation marks.
3. Get the variable value
To obtain the value of a variable, you only need to add $ before the variable name or use $ {} to enclose the variable.
# Echo $ path
# Echo $ {path}
4. Cancel and clear Variables
When you no longer need a variable, you may want to cancel the variable, delete it from the current namespace and release the memory occupied by the variable. In bash shell, you can use the unset command to cancel a variable. The usage is as follows:
Unset variable nameOrUnset-V variable name
-V indicates canceling the variable. unset can be used to cancel the variable and cancel the function. When unset is used to cancel the function, the usage is as follows:
Unset-f function name
After unset is used, the variable no longer exists. This may not be what you want. You may just want to clear the value in the variable and make it null, that is, clear the variable, to clear a variable, perform the following operations:
Variable name =
5. Environment Variables
Only when a variable becomes an environment variable can it be used by the sub-shell. To make a variable an environment variable, you need to use the Export command as follows:
Variable name = "XXXX"
Export variable name
Or
Export variable name = "XXXX"
In addition to using export, you can also specify the variable as an environment variable during declaration, as shown below:
Declare-x variable name
6. built-in bash Variables
In addition to environment variables and User-Defined variables, bash shell also uses many built-in variables. The following describes some common built-in variables.
Bash-- Complete bash path, usually/bin/bash
Bash_version-- Bash version
Bash_env-- In non-interactive mode, the system checks whether $ bash_env has a specified Startup File. If yes, it executes
Env-- Similar to bash_env, but in POSIX mode, $ ENV checks whether a specified Startup file exists. If yes, run it first.
Cdpath-- CD command search path
Path-- Command search path
EUID-- Valid user ID
Funcname-- During function execution, it is the name of the function itself.
Hostname-- Host Name
Hosttype-- Host type, such as i386
Ostype-- The type of the bash operating system, such as Linux-GNU
Home-- User main directory
IFS-- Default field separator
Optarg-- The parameter of the Option obtained when getopts is used to process the options
Optind-- The index value of the option when the getopts processing option is used
Opterr-- If opterr is set to 1, an error occurs in getopts and an error message is output.
$1 ~ $ N-- Location parameter, that is, the input program or function parameter. $1 is the first parameter, $2 is the second parameter, and so on.
$ *-- All location parameters, and regard them as a string, such as "test. Sh abc 123", then $ * is "abc 123"
[Email protected]-- All location parameters, and regard them as a string array, for example, "test. Sh abc 123", $ * is "abc 123"
$ #-- Number of location parameters
$?-- Return value after execution of the previous command
$-- Current BASH Shell Process number
$!-- Process number of the previous background program
7. Adjust the attributes of Variables
The declare command can be used not only to declare variables, but also to adjust the attributes of variables. The specific usage is as follows:
-P: Display attributes of Variables
-The a variable is an array.
-The I variable is an integer.
-The R variable is read-only.
-The X variable is the environment variable.
Shell variables and string operations