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

70,197

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Share:
70,197
Nagel
Author by

Nagel

Updated on October 01, 2020

Comments

  • Nagel
    Nagel over 3 years

    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.

  • Nagel
    Nagel over 12 years
    Great! So, if I understand you correctly, I can safely remove any single semicolons at the end of any line, but never double ones?
  • jvriesem
    jvriesem almost 6 years
    What's the practical difference between ; and ;;, then? I'm not familiar enough with BASH syntax parsing to know the practical difference between what BASH calls a "metacharacter" and what it calls a "control operator".
  • grgry
    grgry almost 6 years
    agree with jvriesem's questions & comments, the docs snippet seems a bit too narrow
  • MaXi32
    MaXi32 almost 3 years
    You could use it as best practice because some popular programming languages like Java use this. So if you keep using this as best practice in bash, you will never forget this in Java.
  • Ardent Coder
    Ardent Coder over 2 years
    @MaXi32 I tried to upvote your comment using my teeth so that I don't forget how to eat :-)