Linux: Shell (中英双语版)

Linux: Shell (中英双语版)

Introduction / 简介
The Shell is the command-line interface that bridges users and the Linux kernel. It interprets your commands and executes them, acting as the most powerful tool for system administration and automation.
Shell 是连接用户与 Linux 内核的命令行界面。它解释并执行您的命令,是系统管理与自动化最强大的工具。


1. Common Shell Types / 常见 Shell 类型

Shell Description / 说明
bash Bourne Again SHell. Default on most Linux distributions. / 大多数 Linux 发行版的默认 Shell。
zsh Z Shell. Enhanced features, popular with Oh My Zsh. / 功能增强,配合 Oh My Zsh 非常流行。
sh Bourne Shell. The original Unix shell, lightweight. / 原始 Unix Shell,轻量级。
fish Friendly Interactive SHell. Auto-suggestions and syntax highlighting. / 交互式友好,支持自动建议与语法高亮。

2. Basic Commands / 基础命令

Command / 命令 Description / 说明 Example / 示例
ls List directory contents / 列出目录内容 ls -la /home
cd Change directory / 切换目录 cd /var/log
pwd Print working directory / 显示当前路径 pwd
mkdir Create directory / 创建目录 mkdir -p project/src
rm Remove files or directories / 删除文件或目录 rm -rf old_backup/
cp Copy files / 复制文件 cp config.yml config.bak
mv Move or rename / 移动或重命名 mv draft.md final.md

3. Pipes & Redirection / 管道与重定向

Pipes (|) pass the output of one command as input to another.
管道 (|) 将前一个命令的输出传递给后一个命令作为输入。

# Find all .log files, sort them, and count lines  
# 查找所有 .log 文件,排序并统计行数  
find /var/log -name "*.log" | sort | wc -l  

Redirection (>, >>, <) controls where input/output goes.
重定向 (>, >>, <) 控制输入/输出的流向。

# Overwrite file / 覆盖文件  
echo "Hello Linux" > greeting.txt  
  
# Append to file / 追加到文件  
echo "More content" >> greeting.txt  
  
# Read from file / 从文件读取  
sort < unsorted_list.txt  

4. Shell Scripting Basics / Shell 脚本基础

A shell script is a text file containing a sequence of commands. Start with a shebang (#!).
Shell 脚本是包含一系列命令的文本文件。以 Shebang (#!) 开头。

#!/bin/bash  
  
# Variables / 变量  
GREETING="Hello, World!"  
USER_NAME=$(whoami)  
  
# Conditional / 条件判断  
if [ -d "/home/$USER_NAME" ]; then  
    echo "$GREETING Home directory exists."  
else  
    echo "Home directory not found."  
fi  
  
# Loop / 循环  
for i in {1..3}; do  
    echo "Count: $i"  
done  

Make it executable and run:
赋予执行权限并运行:

chmod +x script.sh  
./script.sh  

5. Environment Variables / 环境变量

Environment variables store system-wide or user-specific settings.
环境变量用于存储系统级或用户级配置。

# View all variables / 查看所有变量  
env  
  
# Set temporarily / 临时设置  
export PATH="/usr/local/bin:$PATH"  
  
# Make persistent / 持久化保存  
echo 'export MY_API_KEY="secret123"' >> ~/.bashrc  
source ~/.bashrc  

6. Useful Tips / 实用技巧

Tip / 技巧 Command / 命令
Command History / 历史命令 history | grep "ssh"
Alias / 别名 alias ll="ls -la"
Background Job / 后台任务 python3 app.py &
Check Process / 查看进程 ps aux | grep nginx
Disk Usage / 磁盘使用 df -hdu -sh *

7. Summary / 总结

The Linux Shell is not just a tool; it's a language for talking to the operating system. Mastering it unlocks efficiency, automation, and deep system understanding.
Linux Shell 不仅是一个工具,更是与操作系统对话的语言。掌握它将为您带来效率、自动化能力与对系统的深层理解。

"In the beginning was the command line." — Neal Stephenson
“起初,只有命令行。” —— 尼尔·斯蒂芬森


雨轩于听雨轩 🌧️🏠