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

后端 未结 5 940
鱼传尺愫
鱼传尺愫 2020-12-12 20:07

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 )
    ec         


        
相关标签:
5条回答
  • 2020-12-12 20:30

    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.

    0 讨论(0)
  • 2020-12-12 20:31

    @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 讨论(0)
  • 2020-12-12 20:39

    @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

    0 讨论(0)
  • 2020-12-12 20:40

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

    0 讨论(0)
  • 2020-12-12 20:45

    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.

    0 讨论(0)
提交回复
热议问题