Shell Bash 脚本常用命令详解269


简介

Shell Bash 是 Linux/Unix 系统中常用的命令行解释器,允许用户通过编写脚本来自动执行任务。Bash 脚本使用各种命令来实现各种功能,以下是 Bash 脚本中最常用的命令:

基本命令

echo


显示文本或变量的值。语法:echo [options] [string]

pwd


显示当前工作目录。语法:pwd

ls


列出当前目录中的文件和目录。语法:ls [options] [path]

cd


更改当前工作目录。语法:cd [directory]

mkdir


创建目录。语法:mkdir [options] [directory]

rmdir


删除空目录。语法:rmdir [options] [directory]

条件语句

if


根据条件执行命令。语法:if [condition]; then commands; fi

else


在 if 语句条件为假时执行命令。语法:else commands

elif


在 if 语句条件为假时,但另一个条件为真时执行命令。语法:elif [condition]; then commands

循环语句

for


遍历列表中的元素并执行命令。语法:for variable in list; do commands; done

while


只要条件为真就执行命令。语法:while [condition]; do commands; done

until


只要条件为假就执行命令。语法:until [condition]; do commands; done

变量操作

declare


声明和设置变量。语法:declare [options] [variable=value]

export


将变量导出到子 shell。语法:export [options] [variable]

readonly


使变量只读。语法:readonly [options] [variable]

unset


取消设置变量。语法:unset [options] [variable]

输入/输出重定向

>


重定向输出到指定文件,覆盖现有内容。语法:command > file

>>


重定向输出到指定文件,追加到现有内容。语法:command >> file

2024-12-23


上一篇:如何使用 Go 语言执行 Bash 脚本并传递参数

下一篇:如何使用 bash 脚本参数在博客园发布文章