
I. Bash Shell Script Fundamentals
Bash Shell Script (Bash脚本) is a scripting language executed by the Unix Shell (Unix命令解释器). It is widely used for system automation, environment configuration, and workflow management on Linux Systems (Linux系统) and High Performance Computing (高性能计算, HPC) environments.
Unlike compiled languages, Bash follows an Interpreter Model (解释执行模型), meaning commands are executed line by line by the shell.
1. What is a Bash Script
1) Core Definition
A Bash Script (Bash脚本) is a text file containing a sequence of shell commands that are executed by the Bash Interpreter (Bash解释器).
Its main purposes include:
1)Automating repetitive command execution 2)Managing Environment Variables (环境变量) 3)Controlling program flow using Conditional Statements (条件语句) and Loops (循环) 4)Serving as initialization scripts in Linux and HPC environments
Therefore, Bash acts as a bridge between system commands and automated workflows.
2. Script Execution Methods
There are three common ways to run a Bash script.
1) Direct Execution with Bash
bash script.shThis launches a Subshell (子Shell) to execute the script.
2) Executable Script
First give execution permission:
chmod +x script.shThen run the script:
./script.shThis uses the system’s Shebang (解释器声明) if defined.
Example:
#!/bin/bash3) Source Execution
source script.shor
. script.shThis executes the script inside the Current Shell (当前Shell).
Execution Behavior Comparison
| Method | New Subshell (子Shell) | Variables Persist (变量保留) |
|---|---|---|
bash script.sh | Yes | No |
./script.sh | Yes | No |
source script.sh | No | Yes |
.bashrc, CUDA paths, or Conda environments.
II. Basic Bash Syntax
1. Variables and Environment Variables
1) Normal Variables
Variables are assigned without spaces around the equals sign.
name="Alice"echo $nameHere:
- Variable Assignment (变量赋值) defines the variable
- Variable Expansion (变量展开) occurs using
$
2) Environment Variables
Environment variables are exported using the export command.
export PATH=/usr/bin:$PATHProperties:
1)Visible to Child Processes (子进程) 2)Frequently used for software configuration such as:
- PATH
- CUDA
- Conda
- MPI
2. Conditional Statements
1) Basic if Structure
if condition; then statementfiExample:
if [ -f file.txt ]; then echo "file exists"fiHere the brackets use the Test Command (测试命令).
2) Common File Test Operators
| Operator | Meaning |
|---|---|
-f | regular file |
-d | directory |
-e | exists |
-x | executable |
These belong to the POSIX Test Syntax (POSIX测试语法).
3. Loop Structures
1) for Loop
The most common loop used to iterate through lists or files.
for f in *.txt; do echo $fdoneTypical use cases:
- File iteration
- Command results
- Parameter lists
2) while Loop
Often used for reading files line by line.
while read line; do echo $linedone < file.txtThis uses Input Redirection (输入重定向).
III. Command Execution Model
1. Subshell Execution
bash script.shCharacteristics:
1)Creates a new Process (进程) 2)Variables do not affect the current shell
2. Source Execution
source script.shor
. script.shPurpose:
Execute script content in the Current Shell Environment (当前Shell环境).
Typical usage scenarios:
.bashrc- environment setup
- CUDA configuration
- HPC module initialization
IV. File and Path Operations
1. File Testing
[ -d dir ][ -f file ]These checks are part of the POSIX Test System (POSIX测试系统).
2. Path Expansion and Wildcards
Example:
~/.bashrc.d/*Meaning:
| Symbol | Meaning |
|---|---|
~ | user home directory |
* | wildcard matching all files |
This mechanism is called Filename Expansion (文件名扩展).
V. Modular Configuration Example
A typical .bashrc uses modular configuration loading.
if [ -d ~/.bashrc.d ]; then for rc in ~/.bashrc.d/*; do if [ -f "$rc" ]; then . "$rc" fi donefiExplanation:
1)Check whether the directory exists
2)Iterate through each script file
3)Execute each script using source
This allows a Modular Configuration System (模块化配置系统).
Bash Scripts (Bash脚本) provide a powerful automation mechanism for Linux systems by combining command execution, environment configuration, and control flow using an interpreter-based shell environment.