Bash分号等于换行符不是真的吗? [英] Bash semicolon being equal to newline is not exactly true?
问题描述
我读过多篇文章,指出类似UNIX的shell中的分号(;
)等于换行.
但是,以下内容使我感到困惑,而且我也没有运气来搜索它.
I've read in multiple articles that semicolon(;
) in UNIX-like shells is equal to a new line.
However, the following is confusing me and I'm having no luck googling it either.
我猜想这是shell中 do
的问题,但是"bash分号do"并不是最适合Google的搜索词组合.
I'm guessing it's an issue with do
in shell, but "bash semicolon do" is not exactly the most google-friendly search term combination.
下面是一个简单的 for
语句.
Below is a simple for
statement.
for i in {1..10}
do
echo "hi"
echo "bye"
done
随着许多Stack Overflow大师的发布,每条换行符都可以用分号代替.
As many Stack Overflow gurus have posted, every newline can be substituted with semicolons.
所以..我们有以下相同"的声明.
So.. we have this following "same" statement.
for i in {1..10}; do; echo "hi"; echo "bye"; done
我们得到:
-bash: syntax error near unexpected token `;'
分号到底是什么?这只是 do
的唯一问题吗?
What exactly is the semicolon? Is this just an unique issue with do
?
推荐答案
查看 / do
循环的的语法
,
for name [ [in [words …] ] ; ] do commands; done
我们可以看到 do
后面紧跟着 commands
,因此在 do
之后使用换行符不会t代替分号,而是空格.
we can see that do
is followed by commands
immediately, so using a newline after do
doesn't replace a semicolon, but a space.
复合命令的说明也说
在大多数情况下,复合命令描述中的命令列表可能与一个命令的其余部分用一个或多个换行符分隔开来,并且可能会在换行符后用分号代替.
In most cases a list of commands in a compound command’s description may be separated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon.
但是没有地方说您可以插入随机分号.每个换行符都可以用分号代替"只是一种笼统的说法,并不正确.
but nowhere does it say that you can insert random semicolons. "Every newline can be substituted with semicolons" is simply too general a statement and not correct.
更多手动证据:在关于命令列表的部分中,它说(强调我的意思):
More manual evidence: in the section about lists of commands, it says (emphasis mine):
list
是一个或多个管道的序列,由操作员;
,&
,&&
或||
,并可选地以;
,&
或newline
.
A
list
is a sequence of one or more pipelines separated by one of the operators;
,&
,&&
, or||
, and optionally terminated by one of;
,&
, or anewline
.
在这些列表运算符中,&
和 ||
具有相同的优先级,后跟;
和&
,它们具有相同的优先级.
Of these list operators, &&
and ||
have equal precedence, followed by ;
and &
, which have equal precedence.
一个或多个换行符的序列可能会出现在列表
中,以分隔命令,等同于分号.
A sequence of one or more newlines may appear in a list
to delimit commands, equivalent to a semicolon.
因此,换行符等效于命令列表中的分号 .
So a newline is equivalent to a semicolon within a list of commands.
这篇关于Bash分号等于换行符不是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!