5. Bash 's operators
5.1 numeric operations and operators
5.1.1 Declare declaring variable types: #declare [+/-] [ option] Variable name
Options |
Description |
- |
set type properties for variable |
+ |
to cancel the Type property of a variable |
-I. |
declaring variables as integers (integer) |
-X |
Declaring a variable as an environment variable |
-P |
Displays the type of the variable being declared. |
(1) Numerical operation--Method 1:
#aa =one #bb=cc= $aa + $bb // declare cc as integral type
(2) Numerical operations-Method 2, using expr or let numeric operations tool.
#dd =$ (expr $aa + $bb) (Note that "+" must have spaces on both sides, where "$ ()" indicates the result of the system command execution within the parentheses)
(3) numeric operation--Method 3:"$ (expression)" or "$[expression" (note that the parentheses from $ () represent system commands , while $ () Double parentheses denote numeric operations ! )
"Programming Experiment" variable declarations and numerical operations
#!/bin/Bashaa= OneBB= A#显示变量aa的类型declare-p aa# set AA as environment variable export aa# again displays the type of the variable AA declare-p aa# Numerical operation: Method 1declare-I.cc= $aa +$BB # Numeric operations: Method 2DD=$(Expr$aa +$bb) #数值运算: Method 3ee=$ (($aa +$bb)) FF=$[$aa +$BB]Echo "cc= $cc"Echo "dd= $dd"Echo "ee= $ee"Echo "ff= $ff"
5.1.2 operator
Priority level |
Operator |
Description |
13 |
-,+ |
Monocular negative, Monocular positive |
12 |
!,~ |
logical non, bitwise inverse, or complement |
11 |
*,/,% |
Multiply, divide, take modulo |
10 |
+,- |
Add, Subtract |
9 |
<<,>> |
Bitwise left SHIFT, bitwise right SHIFT |
8 |
< =, > =,<,> |
Less than or equal to, greater than or equal to, less than, greater than |
7 |
==,!= |
Equal to, not equal to |
6 |
& |
Bitwise-AND |
5 |
^ |
Bitwise XOR OR |
4 |
| |
Bitwise OR |
3 |
&& |
Logic and |
2 |
|| |
Logical OR |
1 |
=,+=,-=,*=,/=,%=,&=,^=,|=,<<=,>>= |
Assignment, operation, and assignment |
5.2 variable testing and content substitution
Variable substitution mode |
Variable y is not set |
Variable y is a null value |
Variable y setting value |
x=${y-New Value} |
x= New Value |
X is empty |
x= $y |
x=${y:-New Value} |
x= New Value |
x= New Value |
x= $y |
x=${y+ New Value} |
X is empty |
x= New Value |
x= New Value |
x=${y:+ New Value} |
X is empty |
X is empty |
x= New Value |
x=${y= New Value} |
x= New Value y= New Value |
X is empty The y= value does not change |
x= $y The Y value does not change |
x=${y:= New Value} |
x= New Value y= New Value |
x= New Value The y= value does not change |
x= $y The Y value does not change |
X=${y? New Value} |
New value output to standard error output (that is, screen) |
X is empty |
x= $y |
X=${y:? New value} |
New value output to standard error output |
New value output to standard error output |
x= $y |
(1) The value of x can be used to determine whether y is set or null
(2) Test x=${y-new Value}
#unset y // Delete variable y#x=${y-new}#echo $x // output New, Because the variable y does not exist, so x=new
6. environment variable configuration file
6.1 configuration file
(1) Source command: force the configuration file to take effect
#source configuration file, or
#. Configuration file (note,"." There is a space between the configuration file )
(2) environment variable configuration file
The environment variable profile is primarily defined as default environment variables for the system 's operating environment, such as path, hinstsize, PS1, hostname , and so on.
6.2 the role of the configuration file
(1) The order in which the configuration files are called
(2) Main configuration files
File |
Role |
Note |
/etc/profile |
①user variable ②logmanae variable ③mail variable ④path variable ⑤hostname, histsize variables ⑥umask ⑦ calling the/etc/profile.d/*.sh file |
Effective for all users |
/etc/profile.d/*.sh |
The ①/etc/profile.d/directory contains some configuration files related to color language and so on. ② Calling/etc/sysconfig/i18n configuration file (locale, such as UTF-8) |
/etc/bashrc |
①PS1 variable (login prompt) ②umask ③path variable ④ calling the/etc/profile.d/*.sh file |
~/.bash_profile |
① called the ~/.BASHRC file ② added: "$HOME/bin" in the directory after the path variable. |
Valid for the current user, put in home directory and are hidden files. |
~/.bashrc |
① Defining default Aliases ② calling the/ETC/BASHRC file |
6.3 additional profile and logon information
(1) environment variable configuration file in effect when logging off : ~/.bash_logout
(2) History command configuration file: ~/bash_history
(3) Shell login information
① Local Terminal welcome message :/etc/issue(only log on locally to see this information)
Escape character |
Role |
\d |
Show current system date |
\s |
Show operating system name |
\l |
Record login terminal number, this is more commonly used |
\m |
Displays hardware architectures such as i386, i686, and so on. |
\ n |
Display host Name |
\o |
Show Domain name |
\ r |
Show Kernel version |
\ t |
Show current system time |
\u |
Displays the serial number of the currently logged on user |
② Remote Terminal welcome information: /etc/issue.net
A. Escape character cannot be used in the/etc/issue.net file
B. Whether this welcome message is displayed is determined by the SSH configuration file/etc/ssh/sshd_config and added to the "banner/etc/issue.net" line to display (to restart the SSH service, #service sshd Restart)
③ Post-login Welcome message:/ETC/MOTD , whether logged in locally or remotely, this welcome message can be displayed. But he is logged in only after the information is displayed, unlike the previous two that are displayed before logging in.
9th Shell Foundation (4) _bash operator and environment variable configuration file