在bash脚本中if表达式结尾处是分号可选的吗?

Geo*_*rge 0 bash

在bash脚本,一个的支架引述表达if语句没有分号的权利],根据部分shell脚本编程本的.

但是分号可以在许多不同的教程或脚本中看到,例如本教程和此脚本片段:

if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi
Run Code Online (Sandbox Code Playgroud)

我什么时候必须在那里加分号?

Joh*_*ica 5

如果你想要then在同一条线上,那么你需要一个分号.如果没有,它是可选的.

if x; then
    y
fi

if x
then
    y
fi
Run Code Online (Sandbox Code Playgroud)

把它想象成你编写两个命令的方式.你也可以写

foo
bar
Run Code Online (Sandbox Code Playgroud)

要么

foo; bar
Run Code Online (Sandbox Code Playgroud)