开发者

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

开发者 https://www.devze.com 2023-04-07 01:54 出处:网络
This question already has answers here: Bash semicolon being equal to newline is not exactly true? (4 answers)
This question already has answers here: Bash semicolon being equal to newline is not exactly true? (4 answers) Closed 2 years ago.

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? A开发者_JAVA技巧nd 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.


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.


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.


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


@Opensourcebook-Amit

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

See the below examples:

On terminal:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号