bash-linux
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBash Linux Patterns
Bash Linux 终端实用模式
Essential patterns for Bash on Linux/macOS.
适用于Linux/macOS的Bash实用模式。
1. Operator Syntax
1. 操作符语法
Chaining Commands
命令链式执行
| Operator | Meaning | Example |
|---|---|---|
| Run sequentially | |
| Run if previous succeeded | |
| Run if previous failed | |
| Pipe output | |
| 操作符 | 含义 | 示例 |
|---|---|---|
| 按顺序执行 | |
| 前一个命令执行成功后再运行 | |
| 前一个命令执行失败后再运行 | |
| 管道输出 | |
2. File Operations
2. 文件操作
Essential Commands
核心命令
| Task | Command |
|---|---|
| List all | |
| Find files | |
| File content | |
| First N lines | |
| Last N lines | |
| Follow log | |
| Search in files | |
| File size | |
| Disk usage | |
| 任务 | 命令 |
|---|---|
| 列出所有文件 | |
| 查找文件 | |
| 查看文件内容 | |
| 查看前N行 | |
| 查看后N行 | |
| 实时跟踪日志 | |
| 在文件中搜索内容 | |
| 查看文件大小 | |
| 查看磁盘使用情况 | |
3. Process Management
3. 进程管理
| Task | Command |
|---|---|
| List processes | |
| Find by name | |
| Kill by PID | |
| Find port user | |
| Kill port | |
| Background | |
| Jobs | |
| Bring to front | |
| 任务 | 命令 |
|---|---|
| 列出所有进程 | |
| 按名称查找进程 | |
| 通过PID杀死进程 | |
| 查找端口占用者 | |
| 杀死端口占用进程 | |
| 后台运行命令 | |
| 查看后台任务 | |
| 将后台任务调至前台 | |
4. Text Processing
4. 文本处理
Core Tools
核心工具
| Tool | Purpose | Example |
|---|---|---|
| Search | |
| Replace | |
| Extract columns | |
| Cut fields | |
| Sort lines | |
| Unique lines | |
| Count | |
| 工具 | 用途 | 示例 |
|---|---|---|
| 内容搜索 | |
| 内容替换 | |
| 提取列 | |
| 截取字段 | |
| 行排序 | |
| 去重行 | |
| 统计 | |
5. Environment Variables
5. 环境变量
| Task | Command |
|---|---|
| View all | |
| View one | |
| Set temporary | |
| Set in script | |
| Add to PATH | |
| 任务 | 命令 |
|---|---|
| 查看所有环境变量 | |
| 查看单个环境变量 | |
| 设置临时环境变量 | |
| 在脚本中设置环境变量 | |
| 添加路径到PATH | |
6. Network
6. 网络操作
| Task | Command |
|---|---|
| Download | |
| API request | |
| POST JSON | |
| Check port | |
| Network info | |
| 任务 | 命令 |
|---|---|
| 下载文件 | |
| API请求 | |
| 发送JSON格式POST请求 | |
| 检查端口连通性 | |
| 查看网络信息 | |
7. Script Template
7. 脚本模板
bash
#!/bin/bash
set -euo pipefail # Exit on error, undefined var, pipe failbash
#!/bin/bash
set -euo pipefail # Exit on error, undefined var, pipe failColors (optional)
Colors (optional)
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
Script directory
Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Functions
Functions
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
Main
Main
main() {
log_info "Starting..."
# Your logic here
log_info "Done!"
}
main "$@"
---main() {
log_info "Starting..."
# Your logic here
log_info "Done!"
}
main "$@"
---8. Common Patterns
8. 常用模式
Check if command exists
检查命令是否存在
bash
if command -v node &> /dev/null; then
echo "Node is installed"
fibash
if command -v node &> /dev/null; then
echo "Node is installed"
fiDefault variable value
变量默认值
bash
NAME=${1:-"default_value"}bash
NAME=${1:-"default_value"}Read file line by line
逐行读取文件
bash
while IFS= read -r line; do
echo "$line"
done < file.txtbash
while IFS= read -r line; do
echo "$line"
done < file.txtLoop over files
遍历文件
bash
for file in *.js; do
echo "Processing $file"
donebash
for file in *.js; do
echo "Processing $file"
done9. Differences from PowerShell
9. 与PowerShell的差异
| Task | PowerShell | Bash |
|---|---|---|
| List files | | |
| Find files | | |
| Environment | | |
| String concat | | |
| Null check | | |
| Pipeline | Object-based | Text-based |
| 任务 | PowerShell | Bash |
|---|---|---|
| 列出文件 | | |
| 查找文件 | | |
| 环境变量 | | |
| 字符串拼接 | | |
| 空值检查 | | |
| 管道 | 基于对象 | 基于文本 |
10. Error Handling
10. 错误处理
Set options
设置选项
bash
set -e # Exit on error
set -u # Exit on undefined variable
set -o pipefail # Exit on pipe failure
set -x # Debug: print commandsbash
set -e # Exit on error
set -u # Exit on undefined variable
set -o pipefail # Exit on pipe failure
set -x # Debug: print commandsTrap for cleanup
清理操作陷阱
bash
cleanup() {
echo "Cleaning up..."
rm -f /tmp/tempfile
}
trap cleanup EXITRemember: Bash is text-based. Usefor success chains,&&for safety, and quote your variables!set -e
bash
cleanup() {
echo "Cleaning up..."
rm -f /tmp/tempfile
}
trap cleanup EXIT注意: Bash是基于文本的工具。使用实现成功链式执行,使用&&提升脚本安全性,记得给变量添加引号!set -e