bash shell 脚本中一行末尾的分号是多余的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7507038/
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-09 21:03:31  来源:igfitidea点击:

Semicolons superfluous at the end of a line in shell scripts?

bashshellsyntax

提问by Nagel

I have a shell script which contains the following:

我有一个包含以下内容的 shell 脚本:

case  in
    0 )
    echo  = 0;
    OUTPUT=3;;
    1 )
    echo  = 1;
    OUTPUT=4;;
    2 )
    echo  = 2;
    OUTPUT=4;;
esac

HID=;
BUNCH=16;
LR=.008;

Are semicolonscompletely superfluous in the snippet above? And is there any reason for some people using double semicolons?

分号完全是多余的在上面的代码片段?有些人使用双分号有什么理由吗?

It appears semicolons are only a separator, something you would use instead of a new line.

看来分号只是一个分隔符,您可以使用它来代替换行符。

回答by Ignacio Vazquez-Abrams

Single semicolons at the end of a line are superfluous, since the newline is also a command separator. casespecifically needs double semicolons at the end of the last command in each pattern block; see help casefor details.

行尾的单个分号是多余的,因为换行符也是命令分隔符。case在每个模式块中的最后一个命令的末尾特别需要双分号;help case详情请参阅。

回答by Micha? ?rajer

According to man bash:

根据man bash

  metacharacter
         A character that, when unquoted, separates words.  One of the following:
         |  & ; ( ) < > space tab
  control operator
         A token that performs a control function.  It is one of the following symbols:
         || & && ; ;; ( ) | |& <newline>
  metacharacter
         A character that, when unquoted, separates words.  One of the following:
         |  & ; ( ) < > space tab
  control operator
         A token that performs a control function.  It is one of the following symbols:
         || & && ; ;; ( ) | |& <newline>

So, the ;can be metacharacter or control operator, while the ;;is always a control operator (in case command).

因此, the;可以是元字符或控制运算符,而 the;;始终是控制运算符(如果是命令)。

In your particular code, all ;at the end of line are not needed. The ;;is needed however.

在您的特定代码中,;不需要所有在行尾的代码。该;;然是必要的。

回答by DanielGKiel

In the special case of find, ;is used to terminate commands invoked by -exec. See the answer of @kenorb to this question.

在 find 的特殊情况下,;用于终止由 -exec 调用的命令。请参阅@kenorb 对此问题的回答

回答by Amit Ganvir

@Opensourcebook-Amit

@Opensourcebook-Amit

newlines equivalent to single semicolon ;on terminal or in shell script.

相当于;终端或 shell 脚本中的单个分号的换行符 。

See the below examples:

请参阅以下示例:

On terminal:

在终端:

[root@server test]# ls;pwd;


On shell script:

在 shell 脚本中:

[root@server test]# cat test4.sh

echo "Current UserName:"
whoami

echo -e "\nCurrent Date:";date;

[root@server test]#

But I am not agree with the comment that &is equivalent to newline or single semicolon

但我不同意&相当于换行符或单个分号的评论

&is run commands in background also a command separator but not worked as semicolon or newline.

&在后台运行命令也是命令分隔符,但不能用作分号或换行符。

回答by I J

@Ignacio Vazquez-Abrams

@伊格纳西奥巴斯克斯-艾布拉姆斯

Actually this is not completely accurate, single semicolons at the end of a line are notsuperfluous and are definitely not the same thing as new lines.

其实这并不完全准确,行尾的单个分号并不是多余的,绝对不是换行。

From the Bash Reference Manual

来自Bash 参考手册

Commands separated by a ‘;' are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

以“;”分隔的命令 依次执行;shell 依次等待每个命令终止。返回状态是最后执行的命令的退出状态。

Commands that are separated by "new line" might beexecuted in parallel when commands separated by semicolon are alwaysexecuted sequentially

当以分号分隔的命令始终按顺序执行时,以“换行”分隔的命令可能会并行执行