Bashrc 中的自定义函数

由于博主经常需要在新的终端里面设置环境变量,比较麻烦,因此设置一下常见的函数,后期会不断扩充。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 设置代理
p1() {
local port="${1:-7890}"
export http_proxy="http://127.0.0.1:${port}"
export https_proxy="http://127.0.0.1:${port}"
echo "代理已开启 (127.0.0.1:${port})"
}

# 关闭代理
p0() {
unset http_proxy
unset https_proxy
echo "代理已关闭"
}


# 设置 CUDA 可见设备
cuda_on() {
if [ -z "$1" ]; then
echo "用法: cuda_on <设备号>"
echo "示例: cuda_on 0 # 使用 GPU 0"
echo " cuda_on 0,2,4 # 使用 GPU 0,2,4"
return 1
fi
export CUDA_VISIBLE_DEVICES="$1"
echo "CUDA_VISIBLE_DEVICES 已设置为: $1"
}

# 恢复所有 GPU 可见
cuda_off() {
unset CUDA_VISIBLE_DEVICES
echo "已恢复所有 GPU 可见"
}

# 查看当前 CUDA 设备设置
cuda_show() {
if [ -z "$CUDA_VISIBLE_DEVICES" ]; then
echo "当前未设置 CUDA_VISIBLE_DEVICES(所有 GPU 可见)"
else
echo "当前 CUDA_VISIBLE_DEVICES = $CUDA_VISIBLE_DEVICES"
fi
}

Bashrc 中的自定义函数
https://d4wnnn.github.io/2026/04/21/Notion/Bashrc 中的自定义函数/
作者
D4wn
发布于
2026年4月21日
许可协议