Bash Basics
About commands and command types
Use the hash table for the command Bash to accelerate the next search. To add a frequently used command, use the hash cmd
Bash executes commands with the same name in the path in the following sequence: built-in commands of the alias keywords function executable files or scripts
When executing a command entered by a user, Bash must first determine the type of the command. You can use type cmd to check whether CMD is of the type.
The enable command is used to determine whether to enable a built-in command. Enable-N cmd can be used to disable a built-in command.
Command is used to remove aliases and function searches.
The builtin command will only look for built-in commands, while ignoring functions and executable files.
Job control: Jobs % 1 fg bg kill stop, etc.
Alias: alias dir = 'dir-l' unalisa dir
Operation directory Stack: dirs pushd popd
File Name replacement: Dir d [1-3] ls. Bash {RC, profile} supports [] pairs of numbers and {} pairs of string Sets
Variable
Variable Declaration: declare-A array-f function-I integer-r read-only-x export variable
The local variable is valid only in the declared shell. the read-only variable cannot be modified unless its attribute is declared again.
Common environment variables:
Bash_version dirstack EUID editor groups histfile histsize home Lang PWD oldpwd path
Ppid PS1-4 random shell uid
Export Var = value-F: the exported variable is the function-N, which converts the global variable to the local variable-P. Only the exported variable is printed.
Non-read-only variables can be cleared through unset,
Variable replacement
$ {Var:-word} if the VaR variable has been set and is not empty, the result is the value of Var. Otherwise, the result is word.
$ {Var: = word} if the VaR variable has been set and is not empty, the result is the value of Var. Otherwise, the VaR variable is set to word.
$ {Var: + word} if the VaR variable has been set and is not empty, set the VaR value to word; otherwise, do not replace it.
$ {Var :? Word} if the VaR variable has been set and is not empty, replace it with word. Otherwise, exit the shell.
Replace $ {var: n} with the substring starting with N.
Replace $ {var: N: Len} with the child string of Len starting from N.
Variable Extension
$ {Var % pattern} remove the minimum matching suffix echo $ {PWD %/*} to display the current parent directory path
Remove the largest matching Suffix from $ {var % pattern }.
$ {Var # pattern} removes the minimum matched prefix.
$ {Var ## pattern} remove the largest matched prefix echo $ {PWD ### */} to display the current directory name
Replace $ {# var} with the variable character count
Special Variables
$ PID of the Current Shell
? Exit status of the previous command
! PID of the previous job executed in the background
Read user input
Read reads input from the terminal and saves it to the built-in variable reply
Read var read input from the terminal into the variable VAR
Read first. Last read multiple variables from the terminal and store them in sequence. If there are many inputs, the last variable becomes a string containing multiple values.
Read-A array reads multiple values from the terminal and saves them to the array.
Read-P pmtstring var returns the prompt string to the terminal, and then reads the input to var.
Read-R line reads a row and allows/
Variable type conversion
If the type of the variable is not determined during the declaration, the variable can dynamically convert the type based on the value assignment, but if the variable type is determined at the declaration, if the value is of another type, this will cause loss or error of original data. Unless the type is declared again.
For integer-type variables, algorithm extensions are supported in the command line, such as num = 3*4. Space is not supported.
Hexadecimal
Var = 16 # ABC VaR is a hexadecimal number ABC
Output
Printf "the number is %. 2f/N" 100
Echo-ne "Hello/nworld/N"-e indicates that escape characters need to be parsed.-N indicates that line breaks are not automatically added.
Location parameters
$0 1-0 $ {10}
$ # Number of parameters for the Value Location
$ * Evaluate all location parameters
"$ *"
$ @
"$ @"
Reference
() Command Group, create sub-shell execution
{} Command group, no sub-shell is created
''Protect all metacharacters from being parsed. to print them, you must put them in double quotation marks or use/escape.
"" Only variables and commands can be replaced to prevent other metacharacters from being parsed.
Command replacement
'Cmd'
$ (CMD)
Both results are a string. If "" is added to the result, the line feed is retained; otherwise, the line feed is lost.
Mathematical Expression Extension
$ [Exp]
$ (Exp ))
F
Array
Declare-A array = (Item1 item2 ...)
Array element reference $ {array [I]}
Reference the entire array $ {array [*]}
Unset Array
Function Definition
Function f ()
{
CMD; cmd;
}
I/O redirection
Find. Name/*. c print> foundit 2> & 1
Command Line Parameters
Set can be used to set location parameters. Use set -- to clear all location parameters
The difference between $ * and $ @ is that when $ * is placed in "", the parameter table becomes a single string, while $ @ is placed in, each parameter is enclosed in quotation marks.
Expression
Evaluation expression
Expr $[3 + 4] $[3 + 4] $(3 + 4 ))
Let arithmetic Extension
Let I = I + 1 supports all C-type operators, but only supports integer operations.
Bash does not support fractional execution. Therefore, you need to run the command in BC or awk and return the result. Bash has no floating point type, so decimal places are represented by strings.
Test expression
Test or [expr1 CMP expr2] can be used for testing. The results are stored in? In the variable, that is, test is equivalent [].
String Testing
[Str1 = str2] str1 and str2 are the same or =
[Str1! = Str2] ST1 and str2 are different
[STR] STR is not empty
[-Z str] The STR length is 0.
[-N str] The length of STR is not 0.
[-L STR] STR Length
[Str1-A str2] and
-O or
! Not
Composite condition test
[[P1 & p2] Both P1 and P2 are true
[[P1 | P2]
[[! P2]
Integer Test
[Num1-EQ num2]
[Numb1-ne num2]
GT Ge lt le
File Property Test
[File1 nt file2] File 1 is newer than file 2
[File1 ot file2]
[File1 EF file2]
[-D file] file is a directory
-B-c-P-l-S block file, character file, pipeline, Link, socket
-E-f file exists, regular file
-G file exists and has a valid GID
-O file exists and has valid uid
-G: Set GID
-K: Set the stick bit.
-R-w-X file readable, writable, and executable
-S file size is not 0
-T file FD opened on the terminal
-U setuid bit settings
The test expression can be equivalent to the Let Operation extension and the C operation extension in (). The latter may be easier to understand, C-like.
Condition Control
If command
Then
Command
Command
Fi
If test expression
Then
Command
Fi
If [String/numeric expression] Then
Command
Fi
If [[string expression] Then
Command
Fi
If (numeric expression ))
If command
Then
Command (s)
Else
Command (s)
Fi
If command
Then
Command (s)
Elif command
Then
Commands (s)
Elif command
Then
Command (s)
Else
Command (s)
Fi
Empty command:
Branch jump
Case variable in
Value1)
Command (s)
;;
Value2)
Command (s)
;;
*)
Command (s)
;;
Esac
Loop
For variable in word_list
Do
Command (s)
Done
While Condition
Do
Command (s)
Done
Until command
Do
Command (s)
Done
Select
Select Program in 'LS-F' PWD date
Do
$ Program
Done
Interrupt Loop
Break [N] jumps out of layer n Loop
Continue [N] continues the n-layer Loop
Capture Signal
Signal list
1) sighup 9) sigkill 17) sigchld 25) sigxfsz
2) SIGINT 10) SIGUSR1 18) sigcont 26) sigvtalrm
3) sigquit 11) SIGSEGV 19) sigstop 27) sigprof
4) sigill 12) sigusr2 20) sigtstp 28) sigwinch
5) sigtrap 13) sigpipe 21) sigttin 29) sigio
6) SIGABRT 14) sigalrm 22) sigttou 30) sigpwr
7) sigbus 15) sigterm 23) sigurg
8) sigfpe 16) sigstkflt 24) sigxcpu
Trap 'COMMAND; command 'signal-number-list
Trap 'COMMAND; command 'signal-name-list processing signal. After receiving the signal from Singal-list, execute the command in''
Trap singal reset signal processing function
Trap list Signal Processing already set
Debug scripts
Bash-x script displays the command execution process and result
Bash-V script displays all rows in the script
Bash-N script interpretation but not execution
Appendix:
Common commands
Script myfile stores the terminal interaction information in myfile and exits with Control + D.
Fuser-n tcp 22 gets the process of enabling TCP 22
Lsof obtains the files opened by the process.
Shortcut Key
CTRL + A to the beginning of the command line
CTRL + C interrupt Command Execution
CTRL + D disable interrupt
CTRL + E to the end of the command line
CTRL + u cut the cursor to the beginning of the line
CTRL + k cut the cursor to the end of the line
CTRL + y paste the deleted character
CTRL + R search in History commands
!! Repeat the last command
! $ Parameters of the Last Command Execution