Semicolons superfluous at the end of a line in she

2019-01-21 08:20发布

I have a shell script which contains the following:

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

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

Are semicolons completely 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.

5条回答
叛逆
2楼-- · 2019-01-21 09:09

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

查看更多
聊天终结者
3楼-- · 2019-01-21 09:15

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

查看更多
\"骚年 ilove
4楼-- · 2019-01-21 09:19

According to 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>

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

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

查看更多
Deceive 欺骗
5楼-- · 2019-01-21 09:24

@Ignacio Vazquez-Abrams

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

From the Bash Reference Manual

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.

Commands that are separated by "new line" might be executed in parallel when commands separated by semicolon are always executed sequentially

查看更多
聊天终结者
6楼-- · 2019-01-21 09:25

@Opensourcebook-Amit

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

See the below examples:

On termainal: [root@server test]# ls;pwd;


On shell script:

[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.

查看更多
登录 后发表回答