Course Content#
Shell & Terminal & Console#
- shell: shell, software, provides an interface to the user
- console: console, workbench
- terminal: interactive device
- Essentially a file
- stdin, stdout, stderr correspond to file descriptors 0, 1, 2 respectively
- 0, 1, 2 are file descriptors; opening any process will open these 3 files
- echo $0: outputs -zsh, indicating the input source is zsh
- stdout, stderr output redirection: 1>file, 2>file
The concepts of console and terminal originate from mainframes; the console can be viewed as a special terminal. Nowadays, the terms are generally used interchangeably.
Linux Help System#
- Two online documents: man (commonly used), info
- man manual sections
Code | Meaning | Example |
---|---|---|
1 | Shell commands or executable files | man 1 ls |
2 | Functions and tools provided by the kernel | man 2 reboot |
3 | Library functions [most of C's function library, not C++, Python...] | man 3 readdir |
4 | Device file descriptions [usually in /dev] | man 4 null |
5 | Configuration files or file formats [like /etc/passwd] | man 5 interfaces |
6 | Games | man 6 lol |
7 | Conventions and protocols [like Linux file systems, network protocols] | man 7 tcp |
8 | System administrator commands [usually for root] | man 8 reboot |
9 | Kernel routines [non-standard routines] | |
o | Old documents | |
n | New documents | |
l | Local documents |
- Learn to read the examples provided in the man manual 👉 best practices
- ❗ Tip: Commands may exist in multiple sections
- Keyword search: man -k reboot
- Exact search: man -f reboot
zsh#
Wildcards#
- ? Any single character
-
- Any number of any characters
- []、{}
Wildcard | Meaning |
---|---|
[123] | Matches any single character in 123 |
[1-3] | Matches any single character in 1-3 |
[0-9a-zA-Z] | Matches any single character in all digits and uppercase/lowercase letters |
[!(1-3)] | Matches any single character except 1-3; in bash, parentheses can be omitted |
{"a","ab",...} | Matches one of the strings "a" or "ab" (or more), cannot have spaces, must have at least two elements |
Task Management#
- & After a command allows it to run in the background
- How to terminate?
- ① fg→ctrl + c
- ② kill task id (can be seen during execution)
- ③ pkill matches name (note: high permissions may cause accidental deletions)
- When entering kill, the terminal's input and output are mixed together, but they actually come from different files: #0, #1
- How to terminate?
- ; Placed between commands for sequential execution
- && Logical AND, note the short-circuit principle
- || Logical OR, note the long-circuit principle
- ` ` Command substitution (note: this key is below esc)
- Executes the command inside first, then passes the result to the parent command
- ctrl + z Suspends the task, at least releases CPU resources
- Whether memory is released: depends on how the underlying system handles swapping, generally swapped to the swap area when memory is insufficient
- Similar to the sleep command
- bg, fg, jobs: See "Linux Introduction and Usage" notes summary — 3. Linux Basic Knowledge — Process Related
Redirection#
- [command] >/>> [file]
- Note: >> appends content to the end of the file, while > overwrites the original file
- [command] < [file]
- Provides the content of the file as input to the command
- <<
- Used to specify the end of input
-
- Here EOF and 000 are just strings, with no special meaning
-
- Used to specify the end of input
Escape Characters#
- Hard escape
- Single quotes wrap ' '
- Any character is treated as is
- 【Note】 Single quotes are not allowed inside the wrapping
- Soft escape
- Double quotes wrap " "
- Except for specific shell metacharacters ($ for variable value substitution, ` for command substitution, \ for escaping a single character), everything else is treated as is
- Backslash
- Escapes, removing the special meaning of the metacharacter or wildcard that follows it
Additional Knowledge Points#
- Variable invocation: $Var is equivalent to ${Var}
- But the latter is more standard, defining the scope of $ can avoid issues when variable names contain special characters
Tips#
- ls --time=[atime, ctime] -l can choose to display access time atime or permission modification time ctime, instead of the default modification time
- Note: Used in conjunction with -l
- For filenames with spaces, zsh may produce misleading results, as follows:
-
- Here single quotes are used to enclose spaces, which do not actually exist!
- To delete in bulk, use sudo rm -i *\ *
- Here -i enables the prompt mode, as this * and \ can be alarming
-