Course Content#
Shell & Terminal & Console#
- shell: shell, software, provides an interface to users
- console: console, workstation
- terminal: interactive device
- Essentially a file
- stdin, stdout, stderr correspond to files 0, 1, 2 respectively
- 0, 1, 2 are file descriptors, opening any process will open these 3 files
- echo $0: output -zsh, indicating that the input source is zsh
- stdout, stderr output redirection: 1>file, 2>file
The concepts of console and terminal originate from mainframes, where console can be seen as a special terminal. Nowadays, they are generally used interchangeably in a broader sense.
Linux Help System#
- Two online documents: man (common) and info
- Man page modules
|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 C function libraries, no C++, Python...]|man 3 readdir|
|4|Device file description [usually in /dev]|man 4 null|
|5|Configuration files or file formats [such as /etc/passwd]|man 5 interfaces|
|6|Games|man 6 lol|
|7|Conventions and protocols [such as Linux file system, network protocols]|man 7 tcp|
|8|System administrator commands [usually used by 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 page for best practices
- ❗ Tip: For commands that may exist in multiple modules
- Keyword search: man -k reboot
- Exact search: man -f reboot
zsh#
Wildcards#
- ? Single arbitrary character
- * Any number of arbitrary 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 numbers, lowercase and uppercase letters |
[!(1-3)] | Matches any single character except 1-3, in bash, parentheses are not required |
{"a","ab",...} | Matches one of the strings "a" or "ab" (or more), no spaces allowed, must have at least two elements |
Task Management#
- & Allows the command to be executed in the background
- How to end it?
- ① fg→ctrl + c
- ② kill task id (visible during execution)
- ③ pkill matching name (note: high privileges may result in accidental deletion)
- When entering kill, the input and output of the terminal are mixed together, but in fact they come from different files: #0, #1
- How to end it?
- ; Placed between commands, executed sequentially
- && Logical AND, pay attention to short-circuiting
- || Logical OR, pay attention to long-circuiting
- ` ` Command substitution symbol (note: this key comes from under esc)
- Executes the commands inside first, then passes the result to the parent command
- ctrl + z Suspends the task, at least it will release CPU resources
- Whether memory is released: depends on how the underlying system handles it, usually when there is not enough memory, it is swapped to the swap area
- Similar to the sleep command
- bg, fg, jobs: See "Linux Introduction and Usage" for details on process-related information
Redirection#
- [command] >/>> [file]
- Note: >> appends the content to the end of the file, while > overwrites the original file with the content
- [command] < [file]
- Provides the content of the file as input to the command
- <<
- Used to specify the end of the file during input
-
- Here, EOF and 000 are just strings and have no special meaning
-
- Used to specify the end of the file during input
Escape Characters#
- Hard escape
- Enclosed in single quotes ' '
- All characters are treated literally
- 【Note】No single quotes are allowed inside the quotes
- Soft escape
- Enclosed in double quotes " "
- Except for specific shell metacharacters ($ for variable value substitution, ` for command substitution, \ for escaping a single character), all characters are treated literally
- Backslash
- Escapes the special meaning of the following metacharacter or wildcard
Additional Knowledge#
- Variable invocation: $Var is equivalent to ${Var}
- However, the latter is more standard and defines the scope of $, which can avoid problems caused by special characters in variable names
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 file names with spaces, zsh may be misleading, as shown below:
-
- The single quotes here are used to enclose the space, but it does not actually exist!
- To delete in batches, use sudo rm -i *\ *
- Here, -i enables interactive mode, because * and \ are scary
-