Reprinted: http://www.bsdlover.cn/html/24/n-3224.html
BashInVariable
1. User-Defined variables
A user-defined variable consists of letters, numbers, and underscores. the first character of the variable name cannot be a number.
Like other UNIX names, variable names are case sensitive.
You can assign values to variables as follows:
Name = Value
When referencing a variable, you must add the $ symbol before it. You can assign values to each other between variables, for example:
(The previous $ is a command prompt)
$ John = John
$ Name = $ John
$ Echo Hello $ name
Hello John
You can also use variables and other characters to form a new word. In this case, you may need to include the variable with {}, for example:
$ Sat = Satur
$ Echo today is $ {sat} day
Today is Saturday
Bash treats unassigned variables with null values. You can use the unset command to clear the variables.
Value.
Bash can also be usedArrayVariable, which has two values:
(1) name [Index] = Value
(2) name = (value1... valuen) The subscript starts from 0.
There is no limit on the range of objects under the array, and continuous components are not required.
The built-in commands for variables in bash include:
(1) declare and typeset have the same functions. The options include:
[-/+] A sets/revokes the array attribute of a variable.
[-/+] I set/undo the integer attribute of a Variable
[-/+] R setting/Revoking the read-only attribute of a Variable
[-/+] X sets/revokes the output attribute of a variable.
-P var: DISPLAY variable attributes
(2) Export and local.
Export outputs the variable to the environment. Usage:
Export Name
Export Name = Value
Here we need to briefly introduce the role of export: When bashShellExecute one
A new execution environment for the program, called the sub-shell,
In bash shell, the variables are local, that is, they are only the sub-
It makes sense in shell. After export is used, the variable is set as a global variable.
Can be recognized by other sub-shells.
The local variable is marked as local (for exampleFunctionInternal use). Usage:
Local name
Local name = Value
(3) readonly.
The specified variable is read-only. After execution, the change quantity cannot be assigned again. Usage:
Readonly name
2. Location variables or shell Parameters
When interpreting USER commands, bash shell uses the first child of the command line as the command, while other words
Parameters are passed to the program through location variables. $1,..., $9 respectively represent the first,..., nine parameters. Among them, 1-9
Is the real parameter name, "$" is only used to identify the replacement of variables.
The position variable $0 indicates the executable name corresponding to the command.
Others include:
$ # Number of parameters sent to the command
$ @ All parameters, each enclosed in double brackets
$ * All parameters, enclosed in double brackets
3. Shell-related variables
(1) Some Common variables set by shell:
Line number of the command being executed by lineno in the script
The full name of the current directory of the PWD user
The full name of the user's current directory before oldpwd last executes CD
Ppid parent process ID
$ Current process ID
Random Number of random (range: 0-32767)
Seconds bash shell running time, in seconds
Reply select and read commands.
Optarg
Ortind is set by the getopt command.
UID: the user ID of the current user.
_ The last parameter used by the previous command
(2) Some common environment variables that affect shell behavior:
The path command is used to search for the path, which is separated by a colon,
The current directory is not in the system path.
The path name of the home directory of the home user. It is the default parameter of the CD command.
Columns defines the length of the command line that can be used in the Command editing mode.
Editor default Line Editor
Visual default visual editor
Fcedit command FC Editor
History of the histfile commandFile
The maximum number of commands that can be included in the history file of the histsize command
The maximum number of lines in the history file of the histfilesize command.
Ifs defines the Separator Used by Shell
LOGNAME
Mail points to a file whose modification time needs to be monitored by shell. After the file is modified,
Shell sends the message you hava mail to the user
Mailcheck shell checks the cycle of the mail file, in seconds
The mailpath function is similar to the mail function. However, you can use a group of files separated by colons.
It can be followed by a question mark and a message sent to the user.
Shell shell path
Term terminal type
The time when tmout shell exits automatically, in seconds. If it is set to 0, shell exits automatically.
Prompt_command specifies the command to be executed before the main command prompt
PS1 main command prompt
PS2 second-level command prompt, which must be used for data input during Command Execution
Command Prompt for PS3 select
PS4 debugging command prompt
Manpath: Find the path of the manual page, separated by a colon
LD_LIBRARY_PATH: Find the library path, separated by a colon
Bash variable assignment
Example: The following similar statements often appear in configure scripts. What do they mean?
If test-n "$ {zsh_version + set}"; then Ac_env_build_alias_set =$ {build_alias + set} Test "$ {ac_configure_args0 + set }"! = Set |
Answers and extensions:
Variable Assignment Method |
STR is not assigned a value. |
STR is a Null String |
STR is a non-empty string |
Remarks |
VaR =$ {str-expr} |
Var = expr |
Var = $ Str |
Set VaR |
VaR =$ {STR + expr} |
Var = $ Str |
Var = expr |
Set var. Neither var nor STR can be set. Different values |
VaR =$ {STR = expr} |
STR = expr Var = expr |
STR unchanged Var = $ Str |
Set VaR and STR, VaR is consistent with Str |
VaR =$ {Str? Expr} |
Expr output to stderr |
Var = Str |
Set VaR |
VaR =$ {STR:-expr} |
Var = expr |
Var = $ Str |
|
VaR =$ {STR: + expr} |
Var = |
Var = expr |
|
VaR =$ {STR: = expr} |
STR = expr Var = expr |
STR unchanged Var = $ Str |
|
VaR =$ {STR :? Expr} |
Expr output to stderr |
Bash string operations
(1) Replacement of strings
(1)
$ {Variable 1/search character/replacement character}
(Note: In this operation, both variables and the first parameter are characters. In addition, this operation does not replace the characters in variable 1. For details, see the example)
Example:
Str1 = abcabcabc123abc
Echo $ {str1/BCA/AAA} # Here, both ABC and AAA are strings, while str1 is a variable. After this operation, the string length in str1 will not be reduced, only a new string is generated.
(2)
$ {Variable 1/# Find characters/replace characters}
(Note: This operation is the same as above, except that it starts from the left and must start with the first character on the left)
Example:
Echo $ {str1/# BCA/AAA} # in this example, BCA is not replaced with AAA because B is not the first character on the left.
Echo $ {str1/# ABC/AAA} #
(3)
$ {Variable 1/% find character/replacement character}
(Opposite to (2), the last character at the end must be matched)
Example:
Echo $ {str1/% 3abc/AAA} # abcabcabc12aaa
(3)
$ {Variable 1 // search for characters/replace characters}
Replace all
(2) Obtain substrings
(1) $ {Variable 1: Location}
(Note: The default value is from the left. If the "position" is negative, it starts with the "position" character on the right and the first position is 0; starting from "position" to getting the substring to the end)
Example:
Str1 = abcabcabc123abc
Echo ${str1 :(-3)} # ABC will be output
(2) $ {Variable 1: Start position: end position}
(3) If "variable 1" is "*" or "@", "location" indicates the first few parameters.
(3) string Movement
(1) $ {string # matching string}
(Note: This is the first matching string on the left, and the shortest "matching string" is removed ")
Example:
Str1 = abcabcabc123
Echo ${str1 # A * c} # output abcabc123
(2) $ {string # matching string}
(Note: This is the first match on the left and the longest "matching string" is removed ")
Str1 = abcabcabc123
Echo ${str1 # A * c} # output 123
Echo ${str1 # B * c} # output abcabcabc123, because no matching starts from the first one
(3) $ {string % matched string}
(4) $ {string % matched string}
(Note that this is the opposite of the above (1) (2), which is matched from the last one)
(4) String Length
$ {# String}
Example:
STR = abcdefg
Echo $ {# STR} # output 7