bash 约定 if ;然后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7985327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:01:24  来源:igfitidea点击:

Bash convention for if ; then

bashshellcoding-style

提问by Chmouel Boudjnah

From this web page :

从这个网页:

http://tldp.org/LDP/abs/html/abs-guide.html

http://tldp.org/LDP/abs/html/abs-guide.html

It's mentioned the usage of the if bracket then convention which need a space after the semicolon :

它提到了 if 括号 then 约定的用法,分号后需要一个空格:

;

Command separator [semicolon]. Permits putting two or more commands on the same line.

echo hello; echo there


if [ -x "$filename" ]; then    #  Note the space after the semicolon.
#+                   ^^
  echo "File $filename exists."; cp $filename $filename.bak
else   #                       ^^
  echo "File $filename not found."; touch $filename
fi; echo "File test complete."

Note that the ";" sometimes needs to be escaped.

;

Command separator [semicolon]. Permits putting two or more commands on the same line.

echo hello; echo there


if [ -x "$filename" ]; then    #  Note the space after the semicolon.
#+                   ^^
  echo "File $filename exists."; cp $filename $filename.bak
else   #                       ^^
  echo "File $filename not found."; touch $filename
fi; echo "File test complete."

Note that the ";" sometimes needs to be escaped.

Does anyone know where is this coming from and if this is needed at all by certain shells?

有谁知道这是从哪里来的,以及某些 shell 是否需要它?

回答by David W.

This has become the style in the last few years:

这已经成为最近几年的风格:

if [ -x "$filename" ]; then
   echo "hi"
fi

However, back when dinosaurs like Burroughs and Sperry Rand ruled the earth, I learned to write if statements like this:

然而,当像 Burroughs 和 Sperry Rand 这样的恐龙统治地球时,我学会了写这样的 if 语句:

if [ -x "$filename" ]
then
    echo "hi"
fi

Then, you don't even need a semicolon.

然后,您甚至不需要分号。

The new style with thenon the same line as the ifstarted in order to emulate the way Cand other programming languages did their ifstatements:

新风格与开始在then同一行,if以模仿C其他编程语言所做的if陈述的方式:

if (! strcmp("foo", "bar")) {
   printf "Strings equal\n";
}

These programming languages put the opening curly brace on the same line as the if.

这些编程语言将左花括号与if.

回答by Roman Cheplyaka

Semicolon ;is an operator(not a keyword, like braces { }or a bang !) in Shell, so it doesn't need to be delimited with white space to be recognized in any POSIX-compliant shell.

分号;是Shell 中的运算符(不是关键字,如大括号{ }或 bang !),因此不需要用空格分隔即可在任何符合 POSIX 的 shell 中识别。

However, doing so improves readability (for my taste).

但是,这样做可以提高可读性(根据我的口味)。

Semicolon needs to be escaped if you mean a symbol "semicolon", not an operator.

如果您的意思是符号“分号”,而不是运算符,则需要转义分号。

回答by Keith Thompson

The space after the semicolon is not required by the syntax for any shell I know of, but it's good style and makes the code easier to read.

我所知道的任何 shell 的语法都不需要分号后面的空格,但它的风格很好,使代码更易于阅读。

I suppose the "sometimes needs to be escaped" wording refers to cases like echo foo\;bar, where you don't want the semicolon to be interpreted as a separator by the shell.

我想“有时需要转义”的措辞指的是像这样的情况echo foo\;bar,您不希望分号被外壳解释为分隔符。

回答by Emil Sit

I do not believe that the space should be necessary there. There's nothing about requiring spaces in the POSIX sh spec.

我认为那里不需要空间。在POSIX sh 规范中没有任何要求空格的内容。

Empirically, the following works fine in both bash4.1.5(1) and dash:

根据经验,以下在bash4.1.5(1) 和dash中都可以正常工作:

$ if true;then echo hi;else echo bye;fi
hi
$ 

回答by Dimitre Radoulov

I've never came across a shell that required a space in that context. Just to make sure, I've asked on c.u.s., you can read the replies here.

我从未遇到过在这种情况下需要空格的外壳。只是为了确保,我已经问过 cus,你可以在这里阅读回复。