786 words
4 minutes
Bash Shell Script

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.

Note: Bash is a Scripting Language (脚本语言), not a compiled language. Scripts are interpreted directly by the shell rather than compiled into machine code.

2. Script Execution Methods#

There are three common ways to run a Bash script.

1) Direct Execution with Bash#

Terminal window
bash script.sh

This launches a Subshell (子Shell) to execute the script.


2) Executable Script#

First give execution permission:

Terminal window
chmod +x script.sh

Then run the script:

Terminal window
./script.sh

This uses the system’s Shebang (解释器声明) if defined.

Example:

#!/bin/bash

3) Source Execution#

Terminal window
source script.sh

or

Terminal window
. script.sh

This executes the script inside the Current Shell (当前Shell).


Execution Behavior Comparison#

MethodNew Subshell (子Shell)Variables Persist (变量保留)
bash script.shYesNo
./script.shYesNo
source script.shNoYes
Note: The source Command (source命令) is commonly used when configuring shell environments such as .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.

Terminal window
name="Alice"
echo $name

Here:

  • Variable Assignment (变量赋值) defines the variable
  • Variable Expansion (变量展开) occurs using $

2) Environment Variables#

Environment variables are exported using the export command.

Terminal window
export PATH=/usr/bin:$PATH

Properties:

1)Visible to Child Processes (子进程) 2)Frequently used for software configuration such as:

  • PATH
  • CUDA
  • Conda
  • MPI

2. Conditional Statements#

1) Basic if Structure#

Terminal window
if condition; then
statement
fi

Example:

Terminal window
if [ -f file.txt ]; then
echo "file exists"
fi

Here the brackets use the Test Command (测试命令).


2) Common File Test Operators#

OperatorMeaning
-fregular file
-ddirectory
-eexists
-xexecutable

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.

Terminal window
for f in *.txt; do
echo $f
done

Typical use cases:

  • File iteration
  • Command results
  • Parameter lists

2) while Loop#

Often used for reading files line by line.

Terminal window
while read line; do
echo $line
done < file.txt

This uses Input Redirection (输入重定向).


III. Command Execution Model#

1. Subshell Execution#

Terminal window
bash script.sh

Characteristics:

1)Creates a new Process (进程) 2)Variables do not affect the current shell


2. Source Execution#

Terminal window
source script.sh

or

Terminal window
. script.sh

Purpose:

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#

Terminal window
[ -d dir ]
[ -f file ]

These checks are part of the POSIX Test System (POSIX测试系统).


2. Path Expansion and Wildcards#

Example:

Terminal window
~/.bashrc.d/*

Meaning:

SymbolMeaning
~user home directory
*wildcard matching all files

This mechanism is called Filename Expansion (文件名扩展).


V. Modular Configuration Example#

A typical .bashrc uses modular configuration loading.

Terminal window
if [ -d ~/.bashrc.d ]; then
for rc in ~/.bashrc.d/*; do
if [ -f "$rc" ]; then
. "$rc"
fi
done
fi

Explanation:

1)Check whether the directory exists 2)Iterate through each script file 3)Execute each script using source

This allows a Modular Configuration System (模块化配置系统).

Note: This design pattern is widely used in Linux distributions and HPC Initialization Scripts (HPC初始化脚本) to organize configuration files into reusable modules.

💡 One-line Takeaway

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.

Bash Shell Script
https://lxy-alexander.github.io/blog/posts/tools/bash-shell-script/
Author
Alexander Lee
Published at
2026-02-07
License
CC BY-NC-SA 4.0