Basic Syntax
Shebang
Variable Assignment
No spaces are allowed around the =
sign.
Double Quotes
Can interpret variables.
Single Quotes
Do not interpret variables, except for single quotes as special characters.
To output single quotes, you need to close single quotes first.
Command Substitution
There are two ways to use backticks and $()
.
It is recommended to use $()
, which is more readable, with more examples as follows:
Here, ls
did not use the -l
option, but the value of count is 4, the reason is here.
Output Redirection
Standard output redirection >
, creates a new file or overwrites an existing one.
Append output redirection >>
.
Standard error redirection 2>
, creates a new file or overwrites an existing one.
Standard output and error redirection to different files.
Standard output and error redirection to the same file.
Input Redirection
Common way, using <
symbol.
Inline redirection, Inline Input Redirection.
FOO is a custom marker, used for multi-line input.
EXPR Command
The inhumane math operation command, spaces around the +
sign cannot be less.
The *
sign is a wildcard, and it needs to be escaped.
Only integer division is supported, no floating-point numbers.
Brackets
You can use []
to perform mathematical operations.
BC Calculator
A precise math operation calculator, full name Basic/Bench Calculator.
Interactive Mode
The -q
option does not print the above English welcome message.
Floating Point
You can directly use floating-point numbers for calculations.
Division uses the scale
variable, default value is 0, indicating integer division.
Indicates to keep 2 decimal places, scale
variable only applies to division.
Pipeline Method
Inline Input Redirection
EXIT Command
The default exit code of the script is 0, indicating normal exit, and can be changed using exit
.
The exit code range is 0-255, mod (%256).